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

Commit 4b4c27df authored by Beverly's avatar Beverly Committed by Beverly Tai
Browse files

Allow some lottie updates close to screen off

So we don't penalize animations that eventually
stop after the screen is off.

Update display state checks to the committed state

Test: manually verified SFPS indicator doesn't
get penalized on screen off
Bug: 420685896
Bug: 431188311
Flag: com.android.systemui.screen_off_animation_guard_enabled

Change-Id: I279ca1c5b4c5b205993c3e56729d7115c539475b
parent 8278ade5
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -304,4 +304,5 @@
    <!-- Used for animation guard tag to throttle number of reported logs. -->
    <item type="id" name="screen_off_animation_guard_set" />
    <item type="id" name="screen_off_animation_guard_reported_wtf" />
    <item type="id" name="screen_off_animation_guard_screen_off_updates" />
</resources>
+30 −6
Original line number Diff line number Diff line
@@ -33,6 +33,7 @@ import com.android.systemui.util.weakReference
import java.lang.ref.WeakReference

private const val LOG_TAG = "AnimationGuard"
private const val LOTTIE_UPDATES_WHEN_SCREEN_OFF_LIMIT = 2

/**
 * This observes a given animation view and reports a WTF if the animations are running while the
@@ -72,11 +73,28 @@ fun LottieAnimationView.enableScreenOffAnimationGuard() {
                        view.id.toString()
                    }

                val isScreenOff = view.display?.state == Display.STATE_OFF
                val isScreenOff = view.display?.committedState == Display.STATE_OFF
                if (isScreenOff) {
                    if (reachedFrameThreshold()) {
                        // These logs create Binder calls, so throttle them. One is enough.
                    Log.wtf(LOG_TAG, "Lottie view $viewIdName is running while screen is off")
                        Log.wtf(
                            LOG_TAG,
                            "Lottie view $viewIdName is running while screen" +
                                " is off; more than $LOTTIE_UPDATES_WHEN_SCREEN_OFF_LIMIT updates",
                        )
                        view.setTag(R.id.screen_off_animation_guard_reported_wtf, true)
                    } else {
                        val updates =
                            (getTag(R.id.screen_off_animation_guard_screen_off_updates) as Int) + 1
                        if (Log.isLoggable(LOG_TAG, Log.DEBUG)) {
                            Log.d(
                                LOG_TAG,
                                "Lottie view $viewIdName is running while " +
                                    "screen is off # updates=$updates",
                            )
                        }
                        setTag(R.id.screen_off_animation_guard_screen_off_updates, updates)
                    }
                } else if (Log.isLoggable(LOG_TAG, Log.DEBUG)) {
                    Log.d(LOG_TAG, "Lottie view $viewIdName is running while screen is on")
                }
@@ -84,10 +102,16 @@ fun LottieAnimationView.enableScreenOffAnimationGuard() {
        }

    setTag(R.id.screen_off_animation_guard_reported_wtf, false)
    setTag(R.id.screen_off_animation_guard_screen_off_updates, 0)
    lottieDrawable.addAnimatorUpdateListener(screenOffListenerGuard)
    setTag(R.id.screen_off_animation_guard_set, System.identityHashCode(lottieDrawable))
}

private fun LottieAnimationView.reachedFrameThreshold(): Boolean {
    val framesRenderedWhenOff = getTag(R.id.screen_off_animation_guard_screen_off_updates) as Int
    return framesRenderedWhenOff >= LOTTIE_UPDATES_WHEN_SCREEN_OFF_LIMIT
}

/**
 * Attaches a listener which will report a [Log.wtf] error if the animator is attempting to render
 * frames while the screen is off.
@@ -98,7 +122,7 @@ fun ValueAnimator.enableScreenOffAnimationGuard(context: Context) {
    }

    val weakContext by weakReference(context)
    enableScreenOffAnimationGuard({ weakContext?.display?.state == Display.STATE_OFF })
    enableScreenOffAnimationGuard({ weakContext?.display?.committedState == Display.STATE_OFF })
}

/** Attaches an animation guard listener to the given ValueAnimator. */
@@ -122,7 +146,7 @@ fun androidx.core.animation.ValueAnimator.enableScreenOffAnimationGuard(context:
    }

    val weakContext by weakReference(context)
    enableScreenOffAnimationGuard({ weakContext?.display?.state == Display.STATE_OFF })
    enableScreenOffAnimationGuard({ weakContext?.display?.committedState == Display.STATE_OFF })
}

/** Attaches an animation guard listener to the given ValueAnimator. */