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

Commit 3ff7e05c authored by Peter_Liang's avatar Peter_Liang
Browse files

Add the failure listener into the lottie animation view of

IllustrationPreference.

Root cause:
The component is unable to parse composition if the lottie file format is not correct.

Solution:
Add the failure listener was provided from lottie animation view to avoid throwing the exception to crash.

Bug: 194223160
Test: manual test in the aosp ROM by forrest build
Change-Id: I8e515d244f904921b9d6f3e6bdf439550bc562a9
parent 84d34f9c
Loading
Loading
Loading
Loading
+12 −16
Original line number Diff line number Diff line
@@ -267,26 +267,22 @@ public class IllustrationPreference extends Preference {

    private static void startLottieAnimationWith(LottieAnimationView illustrationView,
            Uri imageUri) {
        try {
        final InputStream inputStream =
                getInputStreamFromUri(illustrationView.getContext(), imageUri);
        illustrationView.setFailureListener(
                result -> Log.w(TAG, "Invalid illustration image uri: " + imageUri, result));
        illustrationView.setAnimation(inputStream, /* cacheKey= */ null);
        illustrationView.setRepeatCount(LottieDrawable.INFINITE);
        illustrationView.playAnimation();
        } catch (IllegalStateException e) {
            Log.w(TAG, "Invalid illustration image uri: " + imageUri, e);
        }
    }

    private static void startLottieAnimationWith(LottieAnimationView illustrationView,
            @RawRes int rawRes) {
        try {
        illustrationView.setFailureListener(
                result -> Log.w(TAG, "Invalid illustration resource id: " + rawRes, result));
        illustrationView.setAnimation(rawRes);
        illustrationView.setRepeatCount(LottieDrawable.INFINITE);
        illustrationView.playAnimation();
        } catch (IllegalStateException e) {
            Log.w(TAG, "Invalid illustration resource id: " + rawRes, e);
        }
    }

    private static void resetAnimations(LottieAnimationView illustrationView) {
+25 −0
Original line number Diff line number Diff line
@@ -18,6 +18,8 @@ package com.android.settingslib.widget;

import static com.google.common.truth.Truth.assertThat;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
@@ -130,4 +132,27 @@ public class IllustrationPreferenceTest {

        verify(drawable).start();
    }

    @Test
    public void playLottieAnimationWithUri_verifyFailureListener() {
        doReturn(null).when(mAnimationView).getDrawable();

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

        verify(mAnimationView).setFailureListener(any());
    }

    @Test
    public void playLottieAnimationWithResource_verifyFailureListener() {
        // fake the valid lottie image
        final int fakeValidResId = 111;
        doNothing().when(mAnimationView).setImageResource(fakeValidResId);
        doReturn(null).when(mAnimationView).getDrawable();

        mPreference.setLottieAnimationResId(fakeValidResId);
        mPreference.onBindViewHolder(mViewHolder);

        verify(mAnimationView).setFailureListener(any());
    }
}