diff --git a/src/com/android/settings/accessibility/AnimatedImagePreference.java b/src/com/android/settings/accessibility/AnimatedImagePreference.java index 2ca13f33fe2..69c0d136cbd 100644 --- a/src/com/android/settings/accessibility/AnimatedImagePreference.java +++ b/src/com/android/settings/accessibility/AnimatedImagePreference.java @@ -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(); + } } diff --git a/tests/robotests/src/com/android/settings/accessibility/AnimatedImagePreferenceTest.java b/tests/robotests/src/com/android/settings/accessibility/AnimatedImagePreferenceTest.java index c3dd7b5af33..b8ab432d2e9 100644 --- a/tests/robotests/src/com/android/settings/accessibility/AnimatedImagePreferenceTest.java +++ b/tests/robotests/src/com/android/settings/accessibility/AnimatedImagePreferenceTest.java @@ -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(mAnimatedImageDrawable).when(mImageView).getDrawable(); + doReturn(drawable).when(mImageView).getDrawable(); mAnimatedImagePreference.setImageUri(mImageUri); mAnimatedImagePreference.onBindViewHolder(mViewHolder); - verify(mAnimatedImageDrawable).start(); + 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(drawable).when(mImageView).getDrawable(); + + mAnimatedImagePreference.setImageUri(mImageUri); + mAnimatedImagePreference.onBindViewHolder(mViewHolder); + + verify(drawable).start(); } @Test