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

Commit d00ba09b authored by Anton Potapov's avatar Anton Potapov Committed by Android (Google) Code Review
Browse files

Merge "Add propogating current state when adding a callback" into main

parents 06a1dce1 edfe2857
Loading
Loading
Loading
Loading
+4 −1
Original line number Diff line number Diff line
@@ -101,7 +101,10 @@ constructor(

    override fun addCallback(callback: QSTile.Callback?) {
        callback ?: return
        synchronized(callbacks) { callbacks.add(callback) }
        synchronized(callbacks) {
            callbacks.add(callback)
            state?.let(callback::onStateChanged)
        }
    }

    override fun removeCallback(callback: QSTile.Callback?) {
+23 −3
Original line number Diff line number Diff line
@@ -30,14 +30,16 @@ class LoopedAnimatable2DrawableWrapper private constructor(private val animatabl

    private val loopedCallback = LoopedCallback()

    private var isLoopedCallbackRegistered: Boolean = false

    override fun start() {
        animatable2.start()
        animatable2.registerAnimationCallback(loopedCallback)
        setLoopingRegistered(true)
    }

    override fun stop() {
        // stop looping if someone stops the animation
        animatable2.unregisterAnimationCallback(loopedCallback)
        setLoopingRegistered(false)
        animatable2.stop()
    }

@@ -49,7 +51,25 @@ class LoopedAnimatable2DrawableWrapper private constructor(private val animatabl
    override fun unregisterAnimationCallback(callback: Animatable2.AnimationCallback): Boolean =
        animatable2.unregisterAnimationCallback(callback)

    override fun clearAnimationCallbacks() = animatable2.clearAnimationCallbacks()
    override fun clearAnimationCallbacks() {
        animatable2.clearAnimationCallbacks()
        // re-register looped callback to maintain looped behaviour. LoopedCallback is a static
        // class and it has no extra references, so it doesn't provoke a memory leak.
        isLoopedCallbackRegistered = false
        setLoopingRegistered(true)
    }

    private fun setLoopingRegistered(isLooping: Boolean) {
        if (isLooping == isLoopedCallbackRegistered) {
            return
        }
        isLoopedCallbackRegistered = isLooping
        if (isLooping) {
            animatable2.registerAnimationCallback(loopedCallback)
        } else {
            animatable2.unregisterAnimationCallback(loopedCallback)
        }
    }

    override fun getConstantState(): ConstantState? =
        drawable!!.constantState?.let(LoopedAnimatable2DrawableWrapper::LoopedDrawableState)
+23 −0
Original line number Diff line number Diff line
@@ -58,13 +58,36 @@ class LoopedAnimatable2DrawableWrapperTest : SysuiTestCase() {
        verify(drawable).registerAnimationCallback(any())
    }

    @Test
    fun multipleStartAddsTheCallbackOnce() {
        underTest.start()
        underTest.start()
        underTest.start()
        underTest.start()

        verify(drawable).registerAnimationCallback(any())
    }

    @Test
    fun stopRemovesTheCallback() {
        underTest.start()

        underTest.stop()

        verify(drawable).unregisterAnimationCallback(any())
    }

    @Test
    fun callbackSurvivesClearAnimationCallbacks() {
        underTest.start()

        underTest.clearAnimationCallbacks()

        verify(drawable).clearAnimationCallbacks()
        // start + re-add after #clearAnimationCallbacks
        verify(drawable, times(2)).registerAnimationCallback(capture(callbackCaptor))
    }

    @Test
    fun animationLooped() {
        underTest.start()