content: package androidx.recyclerview.widget;
  
  import android.util.DisplayMetrics;
  import android.view.View;
  import android.view.animation.DecelerateInterpolator;
  import android.widget.Scroller;
  import androidx.recyclerview.widget.RecyclerView;
  
  public abstract class SnapHelper extends RecyclerView.OnFlingListener {
      static final float MILLISECONDS_PER_INCH = 100.0f;
      private Scroller mGravityScroller;
      RecyclerView mRecyclerView;
      private final RecyclerView.OnScrollListener mScrollListener = new RecyclerView.OnScrollListener() {
          boolean mScrolled = false;
  
          public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
              super.onScrollStateChanged(recyclerView, newState);
              if (newState == 0 && this.mScrolled) {
                  this.mScrolled = false;
                  SnapHelper.this.snapToTargetExistingView();
              }
          }
  
          public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
              if (dx != 0 || dy != 0) {
                  this.mScrolled = true;
              }
          }
      };
  
      private void destroyCallbacks() {
          this.mRecyclerView.removeOnScrollListener(this.mScrollListener);
          this.mRecyclerView.setOnFlingListener((RecyclerView.OnFlingListener) null);
      }
  
      private void setupCallbacks() throws IllegalStateException {
          if (this.mRecyclerView.getOnFlingListener() == null) {
              this.mRecyclerView.addOnScrollListener(this.mScrollListener);
              this.mRecyclerView.setOnFlingListener(this);
              return;
          }
          throw new IllegalStateException("An instance of OnFlingListener already set.");
      }
  
      private boolean snapFromFling(RecyclerView.LayoutManager layoutManager, int velocityX, int velocityY) {
          RecyclerView.SmoothScroller createScroller;
          int findTargetSnapPosition;
          if (!(layoutManager instanceof RecyclerView.SmoothScroller.ScrollVectorProvider) || (createScroller = createScroller(layoutManager)) == null || (findTargetSnapPosition = findTargetSnapPosition(layoutManager, velocityX, velocityY)) == -1) {
              return false;
          }
          createScroller.setTargetPosition(findTargetSnapPosition);
          layoutManager.startSmoothScroll(createScroller);
          return true;
      }
  
      public void attachToRecyclerView(RecyclerView recyclerView) throws IllegalStateException {
          RecyclerView recyclerView2 = this.mRecyclerView;
          if (recyclerView2 != recyclerView) {
              if (recyclerView2 != null) {
                  destroyCallbacks();
              }
              this.mRecyclerView = recyclerView;
              if (recyclerView != null) {
                  setupCallbacks();
                  this.mGravityScroller = new Scroller(this.mRecyclerView.getContext(), new DecelerateInterpolator());
                  snapToTargetExistingView();
              }
          }
      }
  
      public abstract int[] calculateDistanceToFinalSnap(RecyclerView.LayoutManager layoutManager, View view);
  
      public int[] calculateScrollDistance(int velocityX, int velocityY) {
          this.mGravityScroller.fling(0, 0, velocityX, velocityY, Integer.MIN_VALUE, Integer.MAX_VALUE, Integer.MIN_VALUE, Integer.MAX_VALUE);
          return new int[]{this.mGravityScroller.getFinalX(), this.mGravityScroller.getFinalY()};
      }
  
      /* access modifiers changed from: protected */
      public RecyclerView.SmoothScroller createScroller(RecyclerView.LayoutManager layoutManager) {
          return createSnapScroller(layoutManager);
      }
  
      /* access modifiers changed from: protected */
      @Deprecated
      public LinearSmoothScroller createSnapScroller(RecyclerView.LayoutManager layoutManager) {
          if (!(layoutManager instanceof RecyclerView.SmoothScroller.ScrollVectorProvider)) {
              return null;
          }
          return new LinearSmoothScroller(this.mRecyclerView.getContext()) {
              /* access modifiers changed from: protected */
              public float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {
                  return SnapHelper.MILLISECONDS_PER_INCH / ((float) displayMetrics.densityDpi);
              }
  
              /* access modifiers changed from: protected */
              public void onTargetFound(View targetView, RecyclerView.State state, RecyclerView.SmoothScroller.Action action) {
                  if (SnapHelper.this.mRecyclerView != null) {
                      SnapHelper snapHelper = SnapHelper.this;
                      int[] calculateDistanceToFinalSnap = snapHelper.calculateDistanceToFinalSnap(snapHelper.mRecyclerView.getLayoutManager(), targetView);
                      int i = calculateDistanceToFinalSnap[0];
                      int i2 = calculateDistanceToFinalSnap[1];
                      int calculateTimeForDeceleration = calculateTimeForDeceleration(Math.max(Math.abs(i), Math.abs(i2)));
                      if (calculateTimeForDeceleration > 0) {
                          action.update(i, i2, calculateTimeForDeceleration, this.mDecelerateInterpolator);
                      }
                  }
              }
          };
      }
  
      public abstract View findSnapView(RecyclerView.LayoutManager layoutManager);
  
      public abstract int findTargetSnapPosition(RecyclerView.LayoutManager layoutManager, int i, int i2);
  
      public boolean onFling(int velocityX, int velocityY) {
          RecyclerView.LayoutManager layoutManager = this.mRecyclerView.getLayoutManager();
          if (layoutManager == null || this.mRecyclerView.getAdapter() == null) {
              return false;
          }
          int minFlingVelocity = this.mRecyclerView.getMinFlingVelocity();
          return (Math.abs(velocityY) > minFlingVelocity || Math.abs(velocityX) > minFlingVelocity) && snapFromFling(layoutManager, velocityX, velocityY);
      }
  
      /* access modifiers changed from: package-private */
      public void snapToTargetExistingView() {
          RecyclerView.LayoutManager layoutManager;
          View findSnapView;
          RecyclerView recyclerView = this.mRecyclerView;
          if (recyclerView != null && (layoutManager = recyclerView.getLayoutManager()) != null && (findSnapView = findSnapView(layoutManager)) != null) {
              int[] calculateDistanceToFinalSnap = calculateDistanceToFinalSnap(layoutManager, findSnapView);
              if (calculateDistanceToFinalSnap[0] != 0 || calculateDistanceToFinalSnap[1] != 0) {
                  this.mRecyclerView.smoothScrollBy(calculateDistanceToFinalSnap[0], calculateDistanceToFinalSnap[1]);
              }
          }
      }
  }
  