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

Commit b1bcedc0 authored by Peter_Liang's avatar Peter_Liang
Browse files

Fixing the animation of drawable couldn't play automatically for the banner.

Root cause:
Only supported AnimatedImageDrawable before.

Solution:
Support all classes which implement Animatable

Bug: 190032215
Test: atest AnimatedImagePreferenceTest
Change-Id: Iba18bfab9a46fd02f642d66a3bb9b9ce513c4456
parent 2076ebf2
Loading
Loading
Loading
Loading
+40 −6
Original line number Diff line number Diff line
@@ -17,7 +17,9 @@
package com.android.settings.accessibility;

import android.content.Context;
import android.graphics.drawable.AnimatedImageDrawable;
import android.graphics.drawable.Animatable;
import android.graphics.drawable.Animatable2;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.widget.ImageView;
@@ -36,6 +38,14 @@ public class AnimatedImagePreference extends Preference {
    private Uri mImageUri;
    private int mMaxHeight = -1;

    private final Animatable2.AnimationCallback mAnimationCallback =
            new Animatable2.AnimationCallback() {
        @Override
        public void onAnimationEnd(Drawable drawable) {
            ((Animatable2) drawable).start();
        }
    };

    AnimatedImagePreference(Context context) {
        super(context);
        setLayoutResource(R.layout.preference_animated_image);
@@ -51,12 +61,10 @@ public class AnimatedImagePreference extends Preference {
        }

        if (mImageUri != null) {
            imageView.setImageURI(mImageUri);
            resetAnimation(imageView.getDrawable());

            final Drawable drawable = imageView.getDrawable();
            if (drawable instanceof AnimatedImageDrawable) {
                ((AnimatedImageDrawable) drawable).start();
            }
            imageView.setImageURI(mImageUri);
            startAnimation(imageView.getDrawable());
        }

        if (mMaxHeight > -1) {
@@ -87,4 +95,30 @@ public class AnimatedImagePreference extends Preference {
            notifyChanged();
        }
    }

    private void startAnimation(Drawable drawable) {
        if (!(drawable instanceof Animatable)) {
            return;
        }

        if (drawable instanceof Animatable2) {
            ((Animatable2) drawable).registerAnimationCallback(mAnimationCallback);
        } else if (drawable instanceof AnimationDrawable) {
            ((AnimationDrawable) drawable).setOneShot(false);
        }

        ((Animatable) drawable).start();
    }

    private void resetAnimation(Drawable drawable) {
        if (!(drawable instanceof Animatable)) {
            return;
        }

        if (drawable instanceof Animatable2) {
            ((Animatable2) drawable).clearAnimationCallbacks();
        }

        ((Animatable) drawable).stop();
    }
}
+31 −7
Original line number Diff line number Diff line
@@ -19,12 +19,15 @@ package com.android.settings.accessibility;
import static com.google.common.truth.Truth.assertThat;

import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;

import android.content.Context;
import android.graphics.drawable.AnimatedImageDrawable;
import android.graphics.drawable.AnimatedVectorDrawable;
import android.graphics.drawable.AnimationDrawable;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
@@ -37,7 +40,6 @@ import com.android.settings.R;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.Spy;
import org.robolectric.RobolectricTestRunner;
@@ -54,9 +56,6 @@ public class AnimatedImagePreferenceTest {
    @Spy
    private ImageView mImageView;

    @Mock
    private AnimatedImageDrawable mAnimatedImageDrawable;

    @Before
    public void init() {
        MockitoAnnotations.initMocks(this);
@@ -72,14 +71,39 @@ public class AnimatedImagePreferenceTest {
    }

    @Test
    public void readImageUri_animatedImage_startAnimation() {
    public void playAnimation_animatedImageDrawable_success() {
        final AnimatedImageDrawable drawable = mock(AnimatedImageDrawable.class);
        doReturn(mImageView).when(mRootView).findViewById(R.id.animated_img);
        doReturn(drawable).when(mImageView).getDrawable();

        mAnimatedImagePreference.setImageUri(mImageUri);
        mAnimatedImagePreference.onBindViewHolder(mViewHolder);

        verify(drawable).start();
    }

    @Test
    public void playAnimation_animatedVectorDrawable_success() {
        final AnimatedVectorDrawable drawable = mock(AnimatedVectorDrawable.class);
        doReturn(mImageView).when(mRootView).findViewById(R.id.animated_img);
        doReturn(drawable).when(mImageView).getDrawable();

        mAnimatedImagePreference.setImageUri(mImageUri);
        mAnimatedImagePreference.onBindViewHolder(mViewHolder);

        verify(drawable).start();
    }

    @Test
    public void playAnimation_animationDrawable_success() {
        final AnimationDrawable drawable = mock(AnimationDrawable.class);
        doReturn(mImageView).when(mRootView).findViewById(R.id.animated_img);
        doReturn(mAnimatedImageDrawable).when(mImageView).getDrawable();
        doReturn(drawable).when(mImageView).getDrawable();

        mAnimatedImagePreference.setImageUri(mImageUri);
        mAnimatedImagePreference.onBindViewHolder(mViewHolder);

        verify(mAnimatedImageDrawable).start();
        verify(drawable).start();
    }

    @Test