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

Commit 2d305f3b authored by Chet Haase's avatar Chet Haase Committed by Android (Google) Code Review
Browse files

Merge "Allow system to disable behavior of pausing animators for bg apps"

parents ab535475 7625f950
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -98,6 +98,7 @@ package android.animation {

  public abstract class Animator implements java.lang.Cloneable {
    method public static long getBackgroundPauseDelay();
    method public static void setAnimatorPausingEnabled(boolean);
    method public static void setBackgroundPauseDelay(long);
  }

+28 −5
Original line number Diff line number Diff line
@@ -53,6 +53,9 @@ public class AnimationHandler {
            new ArrayList<>();
    private AnimationFrameCallbackProvider mProvider;

    // Static flag which allows the pausing behavior to be globally disabled/enabled.
    private static boolean sAnimatorPausingEnabled = true;

    /**
     * This paused list is used to store animators forcibly paused when the activity
     * went into the background (to avoid unnecessary background processing work).
@@ -93,6 +96,15 @@ public class AnimationHandler {
        return sAnimatorHandler.get();
    }

    /**
     * Disable the default behavior of pausing infinite animators when
     * apps go into the background.
     *
     * @param enable Enable (default behavior) or disable background pausing behavior.
     */
    public static void setAnimatorPausingEnabled(boolean enable) {
        sAnimatorPausingEnabled = enable;
    }

    /**
     * This is called when a window goes away. We should remove
@@ -136,16 +148,19 @@ public class AnimationHandler {
        } else {
            mAnimatorRequestors.remove(requestor);
        }
        if (!sAnimatorPausingEnabled) {
            // Resume any animators that have been paused in the meantime, otherwise noop
            // Leave logic above so that if pausing gets re-enabled, the state of the requestors
            // list is valid
            resumeAnimators();
            return;
        }
        boolean isEmpty = mAnimatorRequestors.isEmpty();
        if (wasEmpty != isEmpty) {
            // only paused/resume animators if there was a visibility change
            if (!isEmpty) {
                // If any requestors are enabled, resume currently paused animators
                Choreographer.getInstance().removeFrameCallback(mPauser);
                for (int i = mPausedAnimators.size() - 1; i >= 0; --i) {
                    mPausedAnimators.get(i).resume();
                }
                mPausedAnimators.clear();
                resumeAnimators();
            } else {
                // Wait before pausing to avoid thrashing animator state for temporary backgrounding
                Choreographer.getInstance().postFrameCallbackDelayed(mPauser,
@@ -160,6 +175,14 @@ public class AnimationHandler {
        }
    }

    private void resumeAnimators() {
        Choreographer.getInstance().removeFrameCallback(mPauser);
        for (int i = mPausedAnimators.size() - 1; i >= 0; --i) {
            mPausedAnimators.get(i).resume();
        }
        mPausedAnimators.clear();
    }

    private Choreographer.FrameCallback mPauser = frameTimeNanos -> {
        if (mAnimatorRequestors.size() > 0) {
            // something enabled animators since this callback was scheduled - bail
+14 −0
Original line number Diff line number Diff line
@@ -92,6 +92,20 @@ public abstract class Animator implements Cloneable {
        return sBackgroundPauseDelay;
    }

    /**
     * Sets the behavior of animator pausing when apps go into the background.
     * This is exposed as a test API for verification, but is intended for use by internal/
     * platform code, potentially for use by a system property that could disable it
     * system wide.
     *
     * @param enable Enable (default behavior) or disable background pausing behavior.
     * @hide
     */
    @TestApi
    public static void setAnimatorPausingEnabled(boolean enable) {
        AnimationHandler.setAnimatorPausingEnabled(enable);
    }

    /**
     * Starts this animation. If the animation has a nonzero startDelay, the animation will start
     * running after that delay elapses. A non-delayed animation will have its initial