content: package androidx.core.widget;
  
  import android.content.res.Resources;
  import android.os.SystemClock;
  import android.util.DisplayMetrics;
  import android.view.MotionEvent;
  import android.view.View;
  import android.view.ViewConfiguration;
  import android.view.animation.AccelerateInterpolator;
  import android.view.animation.AnimationUtils;
  import android.view.animation.Interpolator;
  import androidx.core.view.ViewCompat;
  
  public abstract class AutoScrollHelper implements View.OnTouchListener {
      private static final int DEFAULT_ACTIVATION_DELAY = ViewConfiguration.getTapTimeout();
      private static final int DEFAULT_EDGE_TYPE = 1;
      private static final float DEFAULT_MAXIMUM_EDGE = Float.MAX_VALUE;
      private static final int DEFAULT_MAXIMUM_VELOCITY_DIPS = 1575;
      private static final int DEFAULT_MINIMUM_VELOCITY_DIPS = 315;
      private static final int DEFAULT_RAMP_DOWN_DURATION = 500;
      private static final int DEFAULT_RAMP_UP_DURATION = 500;
      private static final float DEFAULT_RELATIVE_EDGE = 0.2f;
      private static final float DEFAULT_RELATIVE_VELOCITY = 1.0f;
      public static final int EDGE_TYPE_INSIDE = 0;
      public static final int EDGE_TYPE_INSIDE_EXTEND = 1;
      public static final int EDGE_TYPE_OUTSIDE = 2;
      private static final int HORIZONTAL = 0;
      public static final float NO_MAX = Float.MAX_VALUE;
      public static final float NO_MIN = 0.0f;
      public static final float RELATIVE_UNSPECIFIED = 0.0f;
      private static final int VERTICAL = 1;
      private int mActivationDelay;
      private boolean mAlreadyDelayed;
      boolean mAnimating;
      private final Interpolator mEdgeInterpolator = new AccelerateInterpolator();
      private int mEdgeType;
      private boolean mEnabled;
      private boolean mExclusive;
      private float[] mMaximumEdges = {Float.MAX_VALUE, Float.MAX_VALUE};
      private float[] mMaximumVelocity = {Float.MAX_VALUE, Float.MAX_VALUE};
      private float[] mMinimumVelocity = {0.0f, 0.0f};
      boolean mNeedsCancel;
      boolean mNeedsReset;
      private float[] mRelativeEdges = {0.0f, 0.0f};
      private float[] mRelativeVelocity = {0.0f, 0.0f};
      private Runnable mRunnable;
      final ClampedScroller mScroller = new ClampedScroller();
      final View mTarget;
  
      private static class ClampedScroller {
          private long mDeltaTime = 0;
          private int mDeltaX = 0;
          private int mDeltaY = 0;
          private int mEffectiveRampDown;
          private int mRampDownDuration;
          private int mRampUpDuration;
          private long mStartTime = Long.MIN_VALUE;
          private long mStopTime = -1;
          private float mStopValue;
          private float mTargetVelocityX;
          private float mTargetVelocityY;
  
          ClampedScroller() {
          }
  
          private float getValueAt(long currentTime) {
              long j = this.mStartTime;
              if (currentTime < j) {
                  return 0.0f;
              }
              long j2 = this.mStopTime;
              if (j2 < 0 || currentTime < j2) {
                  return AutoScrollHelper.constrain(((float) (currentTime - j)) / ((float) this.mRampUpDuration), 0.0f, 1.0f) * 0.5f;
              }
              long j3 = currentTime - j2;
              float f = this.mStopValue;
              return (1.0f - f) + (f * AutoScrollHelper.constrain(((float) j3) / ((float) this.mEffectiveRampDown), 0.0f, 1.0f));
          }
  
          private float interpolateValue(float value) {
              return (-4.0f * value * value) + (4.0f * value);
          }
  
          public void computeScrollDelta() {
              if (this.mDeltaTime != 0) {
                  long currentAnimationTimeMillis = AnimationUtils.currentAnimationTimeMillis();
                  float interpolateValue = interpolateValue(getValueAt(currentAnimationTimeMillis));
                  long j = currentAnimationTimeMillis - this.mDeltaTime;
                  this.mDeltaTime = currentAnimationTimeMillis;
                  this.mDeltaX = (int) (((float) j) * interpolateValue * this.mTargetVelocityX);
                  this.mDeltaY = (int) (((float) j) * interpolateValue * this.mTargetVelocityY);
                  return;
              }
              throw new RuntimeException("Cannot compute scroll delta before calling start()");
          }
  
          public int getDeltaX() {
              return this.mDeltaX;
          }
  
          public int getDeltaY() {
              return this.mDeltaY;
          }
  
          public int getHorizontalDirection() {
              float f = this.mTargetVelocityX;
              return (int) (f / Math.abs(f));
          }
  
          public int getVerticalDirection() {
              float f = this.mTargetVelocityY;
              return (int) (f / Math.abs(f));
          }
  
          public boolean isFinished() {
              return this.mStopTime > 0 && AnimationUtils.currentAnimationTimeMillis() > this.mStopTime + ((long) this.mEffectiveRampDown);
          }
  
          public void requestStop() {
              long currentAnimationTimeMillis = AnimationUtils.currentAnimationTimeMillis();
              this.mEffectiveRampDown = AutoScrollHelper.constrain((int) (currentAnimationTimeMillis - this.mStartTime), 0, this.mRampDownDuration);
              this.mStopValue = getValueAt(currentAnimationTimeMillis);
              this.mStopTime = currentAnimationTimeMillis;
          }
  
          public void setRampDownDuration(int durationMillis) {
              this.mRampDownDuration = durationMillis;
          }
  
          public void setRampUpDuration(int durationMillis) {
              this.mRampUpDuration = durationMillis;
          }
  
          public void setTargetVelocity(float x, float y) {
              this.mTargetVelocityX = x;
              this.mTargetVelocityY = y;
          }
  
          public void start() {
              long currentAnimationTimeMillis = AnimationUtils.currentAnimationTimeMillis();
              this.mStartTime = currentAnimationTimeMillis;
              this.mStopTime = -1;
              this.mDeltaTime = currentAnimationTimeMillis;
              this.mStopValue = 0.5f;
              this.mDeltaX = 0;
              this.mDeltaY = 0;
          }
      }
  
      private class ScrollAnimationRunnable implements Runnable {
          ScrollAnimationRunnable() {
          }
  
          public void run() {
              if (AutoScrollHelper.this.mAnimating) {
                  if (AutoScrollHelper.this.mNeedsReset) {
                      AutoScrollHelper.this.mNeedsReset = false;
                      AutoScrollHelper.this.mScroller.start();
                  }
                  ClampedScroller clampedScroller = AutoScrollHelper.this.mScroller;
                  if (clampedScroller.isFinished() || !AutoScrollHelper.this.shouldAnimate()) {
                      AutoScrollHelper.this.mAnimating = false;
                      return;
                  }
                  if (AutoScrollHelper.this.mNeedsCancel) {
                      AutoScrollHelper.this.mNeedsCancel = false;
                      AutoScrollHelper.this.cancelTargetTouch();
                  }
                  clampedScroller.computeScrollDelta();
                  AutoScrollHelper.this.scrollTargetBy(clampedScroller.getDeltaX(), clampedScroller.getDeltaY());
                  ViewCompat.postOnAnimation(AutoScrollHelper.this.mTarget, this);
              }
          }
      }
  
      public AutoScrollHelper(View target) {
          this.mTarget = target;
          DisplayMetrics displayMetrics = Resources.getSystem().getDisplayMetrics();
          int i = (int) ((displayMetrics.density * 1575.0f) + 0.5f);
          int i2 = (int) ((displayMetrics.density * 315.0f) + 0.5f);
          setMaximumVelocity((float) i, (float) i);
          setMinimumVelocity((float) i2, (float) i2);
          setEdgeType(1);
          setMaximumEdges(Float.MAX_VALUE, Float.MAX_VALUE);
          setRelativeEdges(0.2f, 0.2f);
          setRelativeVelocity(1.0f, 1.0f);
          setActivationDelay(DEFAULT_ACTIVATION_DELAY);
          setRampUpDuration(500);
          setRampDownDuration(500);
      }
  
      private float computeTargetVelocity(int direction, float coordinate, float srcSize, float dstSize) {
          float edgeValue = getEdgeValue(this.mRelativeEdges[direction], srcSize, this.mMaximumEdges[direction], coordinate);
          if (edgeValue == 0.0f) {
              return 0.0f;
          }
          float f = this.mRelativeVelocity[direction];
          float f2 = this.mMinimumVelocity[direction];
          float f3 = this.mMaximumVelocity[direction];
          float f4 = f * dstSize;
          return edgeValue > 0.0f ? constrain(edgeValue * f4, f2, f3) : -constrain((-edgeValue) * f4, f2, f3);
      }
  
      static float constrain(float value, float min, float max) {
          return value > max ? max : value < min ? min : value;
      }
  
      static int constrain(int value, int min, int max) {
          return value > max ? max : value < min ? min : value;
      }
  
      private float constrainEdgeValue(float current, float leading) {
          if (leading == 0.0f) {
              return 0.0f;
          }
          int i = this.mEdgeType;
          switch (i) {
              case 0:
              case 1:
                  if (current < leading) {
                      return current >= 0.0f ? 1.0f - (current / leading) : (!this.mAnimating || i != 1) ? 0.0f : 1.0f;
                  }
                  break;
              case 2:
                  if (current < 0.0f) {
                      return current / (-leading);
                  }
                  break;
          }
      }
  
      private float getEdgeValue(float relativeValue, float size, float maxValue, float current) {
          float f;
          float constrain = constrain(relativeValue * size, 0.0f, maxValue);
          float constrainEdgeValue = constrainEdgeValue(size - current, constrain) - constrainEdgeValue(current, constrain);
          if (constrainEdgeValue < 0.0f) {
              f = -this.mEdgeInterpolator.getInterpolation(-constrainEdgeValue);
          } else if (constrainEdgeValue <= 0.0f) {
              return 0.0f;
          } else {
              f = this.mEdgeInterpolator.getInterpolation(constrainEdgeValue);
          }
          return constrain(f, -1.0f, 1.0f);
      }
  
      private void requestStop() {
          if (this.mNeedsReset) {
              this.mAnimating = false;
          } else {
              this.mScroller.requestStop();
          }
      }
  
      private void startAnimating() {
          int i;
          if (this.mRunnable == null) {
              this.mRunnable = new ScrollAnimationRunnable();
          }
          this.mAnimating = true;
          this.mNeedsReset = true;
          if (this.mAlreadyDelayed || (i = this.mActivationDelay) <= 0) {
              this.mRunnable.run();
          } else {
              ViewCompat.postOnAnimationDelayed(this.mTarget, this.mRunnable, (long) i);
          }
          this.mAlreadyDelayed = true;
      }
  
      public abstract boolean canTargetScrollHorizontally(int i);
  
      public abstract boolean canTargetScrollVertically(int i);
  
      /* access modifiers changed from: package-private */
      public void cancelTargetTouch() {
          long uptimeMillis = SystemClock.uptimeMillis();
          MotionEvent obtain = MotionEvent.obtain(uptimeMillis, uptimeMillis, 3, 0.0f, 0.0f, 0);
          this.mTarget.onTouchEvent(obtain);
          obtain.recycle();
      }
  
      public boolean isEnabled() {
          return this.mEnabled;
      }
  
      public boolean isExclusive() {
          return this.mExclusive;
      }
  
      public boolean onTouch(View v, MotionEvent event) {
          if (!this.mEnabled) {
              return false;
          }
          switch (event.getActionMasked()) {
              case 0:
                  this.mNeedsCancel = true;
                  this.mAlreadyDelayed = false;
                  break;
              case 1:
              case 3:
                  requestStop();
                  break;
              case 2:
                  break;
          }
          this.mScroller.setTargetVelocity(computeTargetVelocity(0, event.getX(), (float) v.getWidth(), (float) this.mTarget.getWidth()), computeTargetVelocity(1, event.getY(), (float) v.getHeight(), (float) this.mTarget.getHeight()));
          if (!this.mAnimating && shouldAnimate()) {
              startAnimating();
          }
          return this.mExclusive && this.mAnimating;
      }
  
      public abstract void scrollTargetBy(int i, int i2);
  
      public AutoScrollHelper setActivationDelay(int delayMillis) {
          this.mActivationDelay = delayMillis;
          return this;
      }
  
      public AutoScrollHelper setEdgeType(int type) {
          this.mEdgeType = type;
          return this;
      }
  
      public AutoScrollHelper setEnabled(boolean enabled) {
          if (this.mEnabled && !enabled) {
              requestStop();
          }
          this.mEnabled = enabled;
          return this;
      }
  
      public AutoScrollHelper setExclusive(boolean exclusive) {
          this.mExclusive = exclusive;
          return this;
      }
  
      public AutoScrollHelper setMaximumEdges(float horizontalMax, float verticalMax) {
          float[] fArr = this.mMaximumEdges;
          fArr[0] = horizontalMax;
          fArr[1] = verticalMax;
          return this;
      }
  
      public AutoScrollHelper setMaximumVelocity(float horizontalMax, float verticalMax) {
          float[] fArr = this.mMaximumVelocity;
          fArr[0] = horizontalMax / 1000.0f;
          fArr[1] = verticalMax / 1000.0f;
          return this;
      }
  
      public AutoScrollHelper setMinimumVelocity(float horizontalMin, float verticalMin) {
          float[] fArr = this.mMinimumVelocity;
          fArr[0] = horizontalMin / 1000.0f;
          fArr[1] = verticalMin / 1000.0f;
          return this;
      }
  
      public AutoScrollHelper setRampDownDuration(int durationMillis) {
          this.mScroller.setRampDownDuration(durationMillis);
          return this;
      }
  
      public AutoScrollHelper setRampUpDuration(int durationMillis) {
          this.mScroller.setRampUpDuration(durationMillis);
          return this;
      }
  
      public AutoScrollHelper setRelativeEdges(float horizontal, float vertical) {
          float[] fArr = this.mRelativeEdges;
          fArr[0] = horizontal;
          fArr[1] = vertical;
          return this;
      }
  
      public AutoScrollHelper setRelativeVelocity(float horizontal, float vertical) {
          float[] fArr = this.mRelativeVelocity;
          fArr[0] = horizontal / 1000.0f;
          fArr[1] = vertical / 1000.0f;
          return this;
      }
  
      /* access modifiers changed from: package-private */
      public boolean shouldAnimate() {
          ClampedScroller clampedScroller = this.mScroller;
          int verticalDirection = clampedScroller.getVerticalDirection();
          int horizontalDirection = clampedScroller.getHorizontalDirection();
          return (verticalDirection != 0 && canTargetScrollVertically(verticalDirection)) || (horizontalDirection != 0 && canTargetScrollHorizontally(horizontalDirection));
      }
  }
  