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

Commit 8ee54d89 authored by Anton Potapov's avatar Anton Potapov
Browse files

Provides a looped animatable drawable wrapper

Test: atest LoopedAnimatable2DrawableWrapperTest
Flag: LEGACY QS_PIPELINE_NEW_TILES DISABLED
Fixes: 299908705
Change-Id: Ied16f62879f122cefbce5ec915fd340572fb0838
parent 7a17d789
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -36,3 +36,7 @@ sealed class Icon {
        override val contentDescription: ContentDescription?,
    ) : Icon()
}

/** Creates [Icon.Loaded] for a given drawable with an optional [contentDescription]. */
fun Drawable.asIcon(contentDescription: ContentDescription? = null): Icon =
    Icon.Loaded(this, contentDescription)
+3 −0
Original line number Diff line number Diff line
@@ -234,6 +234,9 @@ constructor(
                disabledByPolicy = viewModelState.enabledState == QSTileState.EnabledState.DISABLED
                expandedAccessibilityClassName = viewModelState.expandedAccessibilityClassName

                // Use LoopedAnimatable2DrawableWrapper to achieve animated tile icon
                isTransient = false

                when (viewModelState.sideViewIcon) {
                    is QSTileState.SideViewIcon.Custom -> {
                        sideViewCustomDrawable =
+94 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.systemui.util.drawable

import android.content.res.Resources
import android.graphics.drawable.Animatable2
import android.graphics.drawable.Drawable
import androidx.appcompat.graphics.drawable.DrawableWrapperCompat

/**
 * Create a looped [Animatable2] restarting it when the animation finishes on its own. Calling
 * [LoopedAnimatable2DrawableWrapper.stop] cancels further looping.
 */
class LoopedAnimatable2DrawableWrapper private constructor(private val animatable2: Animatable2) :
    DrawableWrapperCompat(animatable2 as Drawable), Animatable2 {

    private val loopedCallback = LoopedCallback()

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

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

    override fun isRunning(): Boolean = animatable2.isRunning

    override fun registerAnimationCallback(callback: Animatable2.AnimationCallback) =
        animatable2.registerAnimationCallback(callback)

    override fun unregisterAnimationCallback(callback: Animatable2.AnimationCallback): Boolean =
        animatable2.unregisterAnimationCallback(callback)

    override fun clearAnimationCallbacks() = animatable2.clearAnimationCallbacks()

    override fun getConstantState(): ConstantState? =
        drawable!!.constantState?.let(LoopedAnimatable2DrawableWrapper::LoopedDrawableState)

    companion object {

        /**
         * Creates [LoopedAnimatable2DrawableWrapper] from a [drawable]. The [drawable] should
         * implement [Animatable2].
         *
         * It supports the following resource tags:
         * - `<animated-image>`
         * - `<animated-vector>`
         */
        fun fromDrawable(drawable: Drawable): LoopedAnimatable2DrawableWrapper {
            require(drawable is Animatable2)
            return LoopedAnimatable2DrawableWrapper(drawable)
        }
    }

    private class LoopedCallback : Animatable2.AnimationCallback() {

        override fun onAnimationEnd(drawable: Drawable?) {
            (drawable as? Animatable2)?.start()
        }
    }

    private class LoopedDrawableState(private val nestedState: ConstantState) : ConstantState() {

        override fun newDrawable(): Drawable = fromDrawable(nestedState.newDrawable())

        override fun newDrawable(res: Resources?): Drawable =
            fromDrawable(nestedState.newDrawable(res))

        override fun newDrawable(res: Resources?, theme: Resources.Theme?): Drawable =
            fromDrawable(nestedState.newDrawable(res, theme))

        override fun canApplyTheme(): Boolean = nestedState.canApplyTheme()

        override fun getChangingConfigurations(): Int = nestedState.changingConfigurations
    }
}
+80 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.systemui.util.drawable

import android.graphics.drawable.Animatable2
import android.graphics.drawable.Drawable
import android.testing.TestableLooper
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.MediumTest
import com.android.systemui.SysuiTestCase
import com.android.systemui.util.mockito.any
import com.android.systemui.util.mockito.capture
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.ArgumentCaptor
import org.mockito.Captor
import org.mockito.Mock
import org.mockito.Mockito.times
import org.mockito.Mockito.verify
import org.mockito.MockitoAnnotations

@MediumTest
@RunWith(AndroidJUnit4::class)
@TestableLooper.RunWithLooper(setAsMainLooper = true)
class LoopedAnimatable2DrawableWrapperTest : SysuiTestCase() {

    @Mock private lateinit var drawable: AnimatedDrawable
    @Captor private lateinit var callbackCaptor: ArgumentCaptor<Animatable2.AnimationCallback>

    private lateinit var underTest: LoopedAnimatable2DrawableWrapper

    @Before
    fun setup() {
        MockitoAnnotations.initMocks(this)

        underTest = LoopedAnimatable2DrawableWrapper.fromDrawable(drawable)
    }

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

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

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

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

    @Test
    fun animationLooped() {
        underTest.start()
        verify(drawable).registerAnimationCallback(capture(callbackCaptor))

        callbackCaptor.value.onAnimationEnd(drawable)

        // underTest.start() + looped start()
        verify(drawable, times(2)).start()
    }

    private abstract class AnimatedDrawable : Drawable(), Animatable2
}