package androidx.dynamicanimation.animation; import android.os.Looper; import android.util.AndroidRuntimeException; import androidx.annotation.MainThread; import androidx.dynamicanimation.animation.DynamicAnimation; public final class SpringAnimation extends DynamicAnimation { private static final float UNSET = Float.MAX_VALUE; private boolean mEndRequested = false; private float mPendingPosition = Float.MAX_VALUE; private SpringForce mSpring = null; public SpringAnimation(FloatValueHolder floatValueHolder) { super(floatValueHolder); } public SpringAnimation(FloatValueHolder floatValueHolder, float f) { super(floatValueHolder); this.mSpring = new SpringForce(f); } public SpringAnimation(K k, FloatPropertyCompat floatPropertyCompat) { super(k, floatPropertyCompat); } public SpringAnimation(K k, FloatPropertyCompat floatPropertyCompat, float f) { super(k, floatPropertyCompat); this.mSpring = new SpringForce(f); } private void sanityCheck() { SpringForce springForce = this.mSpring; if (springForce != null) { double finalPosition = (double) springForce.getFinalPosition(); if (finalPosition > ((double) this.mMaxValue)) { throw new UnsupportedOperationException("Final position of the spring cannot be greater than the max value."); } else if (finalPosition < ((double) this.mMinValue)) { throw new UnsupportedOperationException("Final position of the spring cannot be less than the min value."); } } else { throw new UnsupportedOperationException("Incomplete SpringAnimation: Either final position or a spring force needs to be set."); } } public void animateToFinalPosition(float f) { if (isRunning()) { this.mPendingPosition = f; return; } if (this.mSpring == null) { this.mSpring = new SpringForce(f); } this.mSpring.setFinalPosition(f); start(); } public boolean canSkipToEnd() { return this.mSpring.mDampingRatio > 0.0d; } @Override // androidx.dynamicanimation.animation.DynamicAnimation @MainThread public void cancel() { super.cancel(); float f = this.mPendingPosition; if (f != Float.MAX_VALUE) { SpringForce springForce = this.mSpring; if (springForce == null) { this.mSpring = new SpringForce(f); } else { springForce.setFinalPosition(f); } this.mPendingPosition = Float.MAX_VALUE; } } @Override // androidx.dynamicanimation.animation.DynamicAnimation public float getAcceleration(float f, float f2) { return this.mSpring.getAcceleration(f, f2); } public SpringForce getSpring() { return this.mSpring; } @Override // androidx.dynamicanimation.animation.DynamicAnimation public boolean isAtEquilibrium(float f, float f2) { return this.mSpring.isAtEquilibrium(f, f2); } public SpringAnimation setSpring(SpringForce springForce) { this.mSpring = springForce; return this; } @Override // androidx.dynamicanimation.animation.DynamicAnimation public void setValueThreshold(float f) { } public void skipToEnd() { if (!canSkipToEnd()) { throw new UnsupportedOperationException("Spring animations can only come to an end when there is damping"); } else if (Looper.myLooper() != Looper.getMainLooper()) { throw new AndroidRuntimeException("Animations may only be started on the main thread"); } else if (this.mRunning) { this.mEndRequested = true; } } @Override // androidx.dynamicanimation.animation.DynamicAnimation @MainThread public void start() { sanityCheck(); this.mSpring.setValueThreshold((double) getValueThreshold()); super.start(); } @Override // androidx.dynamicanimation.animation.DynamicAnimation public boolean updateValueAndVelocity(long j) { if (this.mEndRequested) { float f = this.mPendingPosition; if (f != Float.MAX_VALUE) { this.mSpring.setFinalPosition(f); this.mPendingPosition = Float.MAX_VALUE; } this.mValue = this.mSpring.getFinalPosition(); this.mVelocity = 0.0f; this.mEndRequested = false; return true; } if (this.mPendingPosition != Float.MAX_VALUE) { long j2 = j / 2; DynamicAnimation.MassState updateValues = this.mSpring.updateValues((double) this.mValue, (double) this.mVelocity, j2); this.mSpring.setFinalPosition(this.mPendingPosition); this.mPendingPosition = Float.MAX_VALUE; DynamicAnimation.MassState updateValues2 = this.mSpring.updateValues((double) updateValues.mValue, (double) updateValues.mVelocity, j2); this.mValue = updateValues2.mValue; this.mVelocity = updateValues2.mVelocity; } else { DynamicAnimation.MassState updateValues3 = this.mSpring.updateValues((double) this.mValue, (double) this.mVelocity, j); this.mValue = updateValues3.mValue; this.mVelocity = updateValues3.mVelocity; } float max = Math.max(this.mValue, this.mMinValue); this.mValue = max; float min = Math.min(max, this.mMaxValue); this.mValue = min; if (!isAtEquilibrium(min, this.mVelocity)) { return false; } this.mValue = this.mSpring.getFinalPosition(); this.mVelocity = 0.0f; return true; } }