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

Commit e31637b6 authored by Nick Chameyev's avatar Nick Chameyev Committed by Android (Google) Code Review
Browse files

Merge "Add screen wakelock tracking to display switch atom" into main

parents cd65552d 385b49ac
Loading
Loading
Loading
Loading
+47 −0
Original line number Diff line number Diff line
@@ -52,6 +52,7 @@ import com.android.systemui.unfold.DisplaySwitchLatencyTracker.Companion.FOLDABL
import com.android.systemui.unfold.DisplaySwitchLatencyTracker.Companion.FOLDABLE_DEVICE_STATE_HALF_OPEN
import com.android.systemui.unfold.DisplaySwitchLatencyTracker.Companion.SCREEN_EVENT_TIMEOUT
import com.android.systemui.unfold.DisplaySwitchLatencyTracker.DisplaySwitchLatencyEvent
import com.android.systemui.unfold.data.repository.ScreenTimeoutPolicyRepository
import com.android.systemui.unfold.data.repository.UnfoldTransitionRepositoryImpl
import com.android.systemui.unfold.domain.interactor.UnfoldTransitionInteractor
import com.android.systemui.unfoldedDeviceState
@@ -97,6 +98,8 @@ class DisplaySwitchLatencyTrackerTest : SysuiTestCase() {
    private val animationStatusRepository = kosmos.fakeAnimationStatusRepository
    private val keyguardInteractor = mock<KeyguardInteractor>()
    private val displaySwitchLatencyLogger = mock<DisplaySwitchLatencyLogger>()
    private val screenTimeoutPolicyRepository = mock<ScreenTimeoutPolicyRepository>()
    private val screenTimeoutActive = MutableStateFlow(true)
    private val latencyTracker = mock<LatencyTracker>()

    private val deviceStateManager = kosmos.deviceStateManager
@@ -136,6 +139,7 @@ class DisplaySwitchLatencyTrackerTest : SysuiTestCase() {
        whenever(resources.getIntArray(R.array.config_foldedDeviceStates))
            .thenReturn(nonEmptyClosedDeviceStatesArray)
        whenever(keyguardInteractor.isAodAvailable).thenReturn(isAodAvailable)
        whenever(screenTimeoutPolicyRepository.screenTimeoutActive).thenReturn(screenTimeoutActive)
        animationStatusRepository.onAnimationStatusChanged(true)
        powerInteractor.setAwakeForTest()
        powerInteractor.setScreenPowerState(SCREEN_ON)
@@ -144,6 +148,7 @@ class DisplaySwitchLatencyTrackerTest : SysuiTestCase() {
                mockContext,
                foldStateRepository,
                powerInteractor,
                screenTimeoutPolicyRepository,
                unfoldTransitionInteractor,
                animationStatusRepository,
                keyguardInteractor,
@@ -196,6 +201,7 @@ class DisplaySwitchLatencyTrackerTest : SysuiTestCase() {
                    mockContext,
                    foldStateRepository,
                    powerInteractor,
                    screenTimeoutPolicyRepository,
                    unfoldTransitionInteractorWithEmptyProgressProvider,
                    animationStatusRepository,
                    keyguardInteractor,
@@ -625,6 +631,44 @@ class DisplaySwitchLatencyTrackerTest : SysuiTestCase() {
        }
    }

    @Test
    fun displaySwitch_screenTimeoutActive_logsNoScreenWakelocks() {
        testScope.runTest {
            startInFoldedState(displaySwitchLatencyTracker)
            screenTimeoutActive.value = true

            startUnfolding()
            advanceTimeBy(100.milliseconds)
            finishUnfolding()

            val event = capturedLogEvent()
            assertThat(event.screenWakelockStatus)
                .isEqualTo(
                    SysUiStatsLog
                        .DISPLAY_SWITCH_LATENCY_TRACKED__SCREEN_WAKELOCK_STATUS__SCREEN_WAKELOCK_STATUS_NO_WAKELOCKS
                )
        }
    }

    @Test
    fun displaySwitch_screenTimeoutNotActive_logsHasScreenWakelocks() {
        testScope.runTest {
            startInFoldedState(displaySwitchLatencyTracker)
            screenTimeoutActive.value = false

            startUnfolding()
            advanceTimeBy(100.milliseconds)
            finishUnfolding()

            val event = capturedLogEvent()
            assertThat(event.screenWakelockStatus)
                .isEqualTo(
                    SysUiStatsLog
                        .DISPLAY_SWITCH_LATENCY_TRACKED__SCREEN_WAKELOCK_STATUS__SCREEN_WAKELOCK_STATUS_HAS_SCREEN_WAKELOCKS
                )
        }
    }

    private fun capturedLogEvent(): DisplaySwitchLatencyEvent {
        verify(displaySwitchLatencyLogger).log(capture(loggerArgumentCaptor))
        return loggerArgumentCaptor.value
@@ -662,6 +706,9 @@ class DisplaySwitchLatencyTrackerTest : SysuiTestCase() {
            fromFoldableDeviceState = fromFoldableDeviceState,
            toFoldableDeviceState = toFoldableDeviceState,
            toState = toState,
            screenWakelockStatus =
                SysUiStatsLog
                    .DISPLAY_SWITCH_LATENCY_TRACKED__SCREEN_WAKELOCK_STATUS__SCREEN_WAKELOCK_STATUS_NO_WAKELOCKS,
            trackingResult = SysUiStatsLog.DISPLAY_SWITCH_LATENCY_TRACKED__TRACKING_RESULT__SUCCESS,
        )
    }
+1 −1
Original line number Diff line number Diff line
@@ -50,7 +50,7 @@ class DisplaySwitchLatencyLogger {
                onScreenTurningOnToOnDrawnMs,
                onDrawnToOnScreenTurnedOnMs,
                trackingResult,
                screenWakelockstatus
                screenWakelockStatus,
            )
        }
    }
+20 −3
Original line number Diff line number Diff line
@@ -36,11 +36,11 @@ import com.android.systemui.power.shared.model.WakeSleepReason
import com.android.systemui.power.shared.model.WakefulnessModel
import com.android.systemui.power.shared.model.WakefulnessState
import com.android.systemui.shared.system.SysUiStatsLog
import com.android.systemui.unfold.DisplaySwitchLatencyTracker.DisplaySwitchLatencyEvent
import com.android.systemui.unfold.DisplaySwitchLatencyTracker.TrackingResult.CORRUPTED
import com.android.systemui.unfold.DisplaySwitchLatencyTracker.TrackingResult.SUCCESS
import com.android.systemui.unfold.DisplaySwitchLatencyTracker.TrackingResult.TIMED_OUT
import com.android.systemui.unfold.dagger.UnfoldSingleThreadBg
import com.android.systemui.unfold.data.repository.ScreenTimeoutPolicyRepository
import com.android.systemui.unfold.data.repository.UnfoldTransitionStatus.TransitionStarted
import com.android.systemui.unfold.domain.interactor.UnfoldTransitionInteractor
import com.android.systemui.util.Compile
@@ -80,6 +80,7 @@ constructor(
    private val context: Context,
    private val deviceStateRepository: DeviceStateRepository,
    private val powerInteractor: PowerInteractor,
    private val screenTimeoutPolicyRepository: ScreenTimeoutPolicyRepository,
    private val unfoldTransitionInteractor: UnfoldTransitionInteractor,
    private val animationStatusRepository: AnimationStatusRepository,
    private val keyguardInteractor: KeyguardInteractor,
@@ -287,7 +288,18 @@ constructor(
        log { "fromFoldableDeviceState=$fromFoldableDeviceState" }
        instantForTrack(TAG) { "fromFoldableDeviceState=$fromFoldableDeviceState" }

        return copy(fromFoldableDeviceState = fromFoldableDeviceState)
        val screenTimeoutActive = screenTimeoutPolicyRepository.screenTimeoutActive.value
        val screenWakelockStatus =
            if (screenTimeoutActive) {
                NO_SCREEN_WAKELOCKS
            } else {
                HAS_SCREEN_WAKELOCKS
            }

        return copy(
            fromFoldableDeviceState = fromFoldableDeviceState,
            screenWakelockStatus = screenWakelockStatus
        )
    }

    private fun DisplaySwitchLatencyEvent.withAfterFields(
@@ -344,7 +356,7 @@ constructor(
        val onDrawnToOnScreenTurnedOnMs: Int = VALUE_UNKNOWN,
        val trackingResult: Int =
            SysUiStatsLog.DISPLAY_SWITCH_LATENCY_TRACKED__TRACKING_RESULT__UNKNOWN_RESULT,
        val screenWakelockstatus: Int =
        val screenWakelockStatus: Int =
            SysUiStatsLog.DISPLAY_SWITCH_LATENCY_TRACKED__SCREEN_WAKELOCK_STATUS__SCREEN_WAKELOCK_STATUS_UNKNOWN,
    )

@@ -372,5 +384,10 @@ constructor(
            SysUiStatsLog.DISPLAY_SWITCH_LATENCY_TRACKED__FROM_FOLDABLE_DEVICE_STATE__STATE_OPENED
        private const val FOLDABLE_DEVICE_STATE_FLIPPED =
            SysUiStatsLog.DISPLAY_SWITCH_LATENCY_TRACKED__FROM_FOLDABLE_DEVICE_STATE__STATE_FLIPPED

        private const val HAS_SCREEN_WAKELOCKS =
            SysUiStatsLog.DISPLAY_SWITCH_LATENCY_TRACKED__SCREEN_WAKELOCK_STATUS__SCREEN_WAKELOCK_STATUS_HAS_SCREEN_WAKELOCKS
        private const val NO_SCREEN_WAKELOCKS =
            SysUiStatsLog.DISPLAY_SWITCH_LATENCY_TRACKED__SCREEN_WAKELOCK_STATUS__SCREEN_WAKELOCK_STATUS_NO_WAKELOCKS
    }
}
+61 −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.unfold.data.repository

import android.os.PowerManager
import android.view.Display
import com.android.systemui.dagger.SysUISingleton
import com.android.systemui.dagger.qualifiers.Background
import com.android.systemui.utils.coroutines.flow.conflatedCallbackFlow
import java.util.concurrent.Executor
import javax.inject.Inject
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.stateIn

/** Repository to get screen timeout updates */
@SysUISingleton
class ScreenTimeoutPolicyRepository
@Inject
constructor(
    private val powerManager: PowerManager,
    @Background private val executor: Executor,
    @Background private val scope: CoroutineScope,
) {

    /** Stores true if there is an active screen timeout */
    val screenTimeoutActive: StateFlow<Boolean> =
        conflatedCallbackFlow {
                val listener =
                    PowerManager.ScreenTimeoutPolicyListener { screenTimeoutPolicy ->
                        trySend(screenTimeoutPolicy == PowerManager.SCREEN_TIMEOUT_ACTIVE)
                    }
                powerManager.addScreenTimeoutPolicyListener(
                    Display.DEFAULT_DISPLAY,
                    executor,
                    listener,
                )
                awaitClose {
                    powerManager.removeScreenTimeoutPolicyListener(
                        Display.DEFAULT_DISPLAY,
                        listener,
                    )
                }
            }
            .stateIn(scope, started = SharingStarted.Eagerly, initialValue = true)
}