Loading packages/SystemUI/src/com/android/systemui/qs/tiles/viewmodel/QSTileViewModelAdapter.kt +4 −1 Original line number Diff line number Diff line Loading @@ -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?) { Loading packages/SystemUI/src/com/android/systemui/util/drawable/LoopedAnimatable2DrawableWrapper.kt +23 −3 Original line number Diff line number Diff line Loading @@ -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() } Loading @@ -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) Loading packages/SystemUI/tests/src/com/android/systemui/util/drawable/LoopedAnimatable2DrawableWrapperTest.kt +23 −0 Original line number Diff line number Diff line Loading @@ -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() Loading Loading
packages/SystemUI/src/com/android/systemui/qs/tiles/viewmodel/QSTileViewModelAdapter.kt +4 −1 Original line number Diff line number Diff line Loading @@ -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?) { Loading
packages/SystemUI/src/com/android/systemui/util/drawable/LoopedAnimatable2DrawableWrapper.kt +23 −3 Original line number Diff line number Diff line Loading @@ -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() } Loading @@ -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) Loading
packages/SystemUI/tests/src/com/android/systemui/util/drawable/LoopedAnimatable2DrawableWrapperTest.kt +23 −0 Original line number Diff line number Diff line Loading @@ -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() Loading