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

Commit eefd14a4 authored by Jonathan Klee's avatar Jonathan Klee
Browse files

Make some tests

parent 3a8864d2
Loading
Loading
Loading
Loading
+11 −36
Original line number Original line Diff line number Diff line
@@ -20,7 +20,6 @@ import android.media.session.MediaController
import android.media.session.PlaybackState
import android.media.session.PlaybackState
import android.os.SystemProperties
import android.os.SystemProperties
import android.util.Log
import android.util.Log
import com.android.internal.annotations.VisibleForTesting
import com.android.systemui.dagger.SysUISingleton
import com.android.systemui.dagger.SysUISingleton
import com.android.systemui.dagger.qualifiers.Main
import com.android.systemui.dagger.qualifiers.Main
import com.android.systemui.statusbar.NotificationMediaManager.isPlayingState
import com.android.systemui.statusbar.NotificationMediaManager.isPlayingState
@@ -30,15 +29,9 @@ import javax.inject.Inject


private const val DEBUG = true
private const val DEBUG = true
private const val TAG = "MediaTimeout"
private const val TAG = "MediaTimeout"

private val PAUSED_MEDIA_TIMEOUT = SystemProperties
@VisibleForTesting
val PAUSED_MEDIA_TIMEOUT = SystemProperties
        .getLong("debug.sysui.media_timeout", TimeUnit.MINUTES.toMillis(10))
        .getLong("debug.sysui.media_timeout", TimeUnit.MINUTES.toMillis(10))


@VisibleForTesting
val RESUME_MEDIA_TIMEOUT = SystemProperties
        .getLong("debug.sysui.media_timeout_resume", TimeUnit.DAYS.toMillis(3))

/**
/**
 * Controller responsible for keeping track of playback states and expiring inactive streams.
 * Controller responsible for keeping track of playback states and expiring inactive streams.
 */
 */
@@ -52,9 +45,8 @@ class MediaTimeoutListener @Inject constructor(


    /**
    /**
     * Callback representing that a media object is now expired:
     * Callback representing that a media object is now expired:
     * @param key Media control unique identifier
     * @param token Media session unique identifier
     * @param timedOut True when expired for {@code PAUSED_MEDIA_TIMEOUT} for active media,
     * @param pauseTimeout True when expired for {@code PAUSED_MEDIA_TIMEOUT}
     *                 or {@code RESUME_MEDIA_TIMEOUT} for resume media
     */
     */
    lateinit var timeoutCallback: (String, Boolean) -> Unit
    lateinit var timeoutCallback: (String, Boolean) -> Unit


@@ -63,7 +55,6 @@ class MediaTimeoutListener @Inject constructor(
        oldKey: String?,
        oldKey: String?,
        data: MediaData,
        data: MediaData,
        immediately: Boolean,
        immediately: Boolean,
        receivedSmartspaceCardLatency: Int,
        isSsReactivated: Boolean
        isSsReactivated: Boolean
    ) {
    ) {
        var reusedListener: PlaybackStateListener? = null
        var reusedListener: PlaybackStateListener? = null
@@ -131,7 +122,6 @@ class MediaTimeoutListener @Inject constructor(


        var timedOut = false
        var timedOut = false
        var playing: Boolean? = null
        var playing: Boolean? = null
        var resumption: Boolean? = null
        var destroyed = false
        var destroyed = false


        var mediaData: MediaData = data
        var mediaData: MediaData = data
@@ -169,20 +159,13 @@ class MediaTimeoutListener @Inject constructor(
        }
        }


        override fun onSessionDestroyed() {
        override fun onSessionDestroyed() {
            // If the session is destroyed, the controller is no longer valid, and we will need to
            // recreate it if this key is updated later
            if (DEBUG) {
            if (DEBUG) {
                Log.d(TAG, "Session destroyed for $key")
                Log.d(TAG, "Session destroyed for $key")
            }
            }

            if (resumption == true) {
                // Some apps create a session when MBS is queried. We should unregister the
                // controller since it will no longer be valid, but don't cancel the timeout
                mediaController?.unregisterCallback(this)
            } else {
                // For active controls, if the session is destroyed, clean up everything since we
                // will need to recreate it if this key is updated later
            destroy()
            destroy()
        }
        }
        }


        private fun processState(state: PlaybackState?, dispatchEvents: Boolean) {
        private fun processState(state: PlaybackState?, dispatchEvents: Boolean) {
            if (DEBUG) {
            if (DEBUG) {
@@ -190,28 +173,20 @@ class MediaTimeoutListener @Inject constructor(
            }
            }


            val isPlaying = state != null && isPlayingState(state.state)
            val isPlaying = state != null && isPlayingState(state.state)
            val resumptionChanged = resumption != mediaData.resumption
            if (playing == isPlaying && playing != null) {
            if (playing == isPlaying && playing != null && !resumptionChanged) {
                return
                return
            }
            }
            playing = isPlaying
            playing = isPlaying
            resumption = mediaData.resumption


            if (!isPlaying) {
            if (!isPlaying) {
                if (DEBUG) {
                if (DEBUG) {
                    Log.v(TAG, "schedule timeout for $key playing $isPlaying, $resumption")
                    Log.v(TAG, "schedule timeout for $key")
                }
                }
                if (cancellation != null && !resumptionChanged) {
                if (cancellation != null) {
                    // if the media changed resume state, we'll need to adjust the timeout length
                    if (DEBUG) Log.d(TAG, "cancellation already exists, continuing.")
                    if (DEBUG) Log.d(TAG, "cancellation already exists, continuing.")
                    return
                    return
                }
                }
                expireMediaTimeout(key, "PLAYBACK STATE CHANGED - $state, $resumption")
                expireMediaTimeout(key, "PLAYBACK STATE CHANGED - $state")
                val timeout = if (mediaData.resumption) {
                    RESUME_MEDIA_TIMEOUT
                } else {
                    PAUSED_MEDIA_TIMEOUT
                }
                cancellation = mainExecutor.executeDelayed({
                cancellation = mainExecutor.executeDelayed({
                    cancellation = null
                    cancellation = null
                    if (DEBUG) {
                    if (DEBUG) {
@@ -220,7 +195,7 @@ class MediaTimeoutListener @Inject constructor(
                    timedOut = true
                    timedOut = true
                    // this event is async, so it's safe even when `dispatchEvents` is false
                    // this event is async, so it's safe even when `dispatchEvents` is false
                    timeoutCallback(key, timedOut)
                    timeoutCallback(key, timedOut)
                }, timeout)
                }, PAUSED_MEDIA_TIMEOUT)
            } else {
            } else {
                expireMediaTimeout(key, "playback started - $state, $key")
                expireMediaTimeout(key, "playback started - $state, $key")
                timedOut = false
                timedOut = false