Merge "Fix initialization of AnimatorSet" into udc-qpr-dev am: 7cd2690bf6

Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/24014148

Change-Id: Icbfa211261b1d5d729f8894ff94c05534725eaba
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
This commit is contained in:
Treehugger Robot
2023-07-12 18:51:54 +00:00
committed by Automerger Merge Worker
2 changed files with 85 additions and 2 deletions

View File

@@ -1346,8 +1346,26 @@ public final class AnimatorSet extends Animator implements AnimationHandler.Anim
}
// Set the child animators to the right end:
if (mShouldResetValuesAtStart) {
initChildren();
skipToEndValue(!mReversing);
if (isInitialized()) {
skipToEndValue(!mReversing);
} else if (mReversing) {
// Reversing but haven't initialized all the children yet.
initChildren();
skipToEndValue(!mReversing);
} else {
// If not all children are initialized and play direction is forward
for (int i = mEvents.size() - 1; i >= 0; i--) {
if (mEvents.get(i).mEvent == AnimationEvent.ANIMATION_DELAY_ENDED) {
Animator anim = mEvents.get(i).mNode.mAnimation;
// Only reset the animations that have been initialized to start value,
// so that if they are defined without a start value, they will get the
// values set at the right time (i.e. the next animation run)
if (anim.isInitialized()) {
anim.skipToEndValue(true);
}
}
}
}
}
if (mReversing || mStartDelay == 0 || mSeekState.isActive()) {

View File

@@ -24,6 +24,7 @@ import static org.junit.Assert.assertTrue;
import android.util.Property;
import android.view.View;
import androidx.annotation.NonNull;
import androidx.test.annotation.UiThreadTest;
import androidx.test.filters.SmallTest;
import androidx.test.rule.ActivityTestRule;
@@ -36,6 +37,8 @@ import org.junit.Rule;
import org.junit.Test;
import java.util.ArrayList;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
@SmallTest
public class AnimatorSetActivityTest {
@@ -613,6 +616,68 @@ public class AnimatorSetActivityTest {
});
}
@Test
public void initAfterStartNotification() throws Throwable {
Property<int[], Integer> property = new Property<>(Integer.class, "firstValue") {
@Override
public Integer get(int[] target) {
throw new IllegalStateException("Shouldn't be called");
}
@Override
public void set(int[] target, Integer value) {
target[0] = value;
}
};
int[] target = new int[1];
ObjectAnimator animator1 = ObjectAnimator.ofInt(target, property, 0, 100);
ObjectAnimator animator2 = ObjectAnimator.ofInt(target, property, 0, 100);
ObjectAnimator animator3 = ObjectAnimator.ofInt(target, property, 0, 100);
animator1.setDuration(10);
animator2.setDuration(10);
animator3.setDuration(10);
AnimatorSet set = new AnimatorSet();
set.playSequentially(animator1, animator2, animator3);
final int[] values = new int[4];
animator2.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationStart(@NonNull Animator animation, boolean isReverse) {
values[0] = target[0];
animator2.setIntValues(target[0], target[0] + 100);
}
@Override
public void onAnimationEnd(@NonNull Animator animation, boolean isReverse) {
values[1] = target[0];
}
});
animator3.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationStart(@NonNull Animator animation, boolean isReverse) {
values[2] = target[0];
animator3.setIntValues(target[0], target[0] + 100);
}
@Override
public void onAnimationEnd(@NonNull Animator animation, boolean isReverse) {
values[3] = target[0];
}
});
final CountDownLatch latch = new CountDownLatch(1);
set.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(@NonNull Animator animation, boolean isReverse) {
latch.countDown();
}
});
mActivityRule.runOnUiThread(() -> set.start());
assertTrue(latch.await(1, TimeUnit.SECONDS));
assertEquals(100, values[0]);
assertEquals(200, values[1]);
assertEquals(200, values[2]);
assertEquals(300, values[3]);
}
/**
* Check that the animator list contains exactly the given animators and nothing else.
*/