discord-jadx/app/src/main/java/androidx/recyclerview/widget/PagerSnapHelper.java

195 lines
8.3 KiB
Java

package androidx.recyclerview.widget;
import android.content.Context;
import android.graphics.PointF;
import android.util.DisplayMetrics;
import android.view.View;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;
public class PagerSnapHelper extends SnapHelper {
private static final int MAX_SCROLL_ON_FLING_DURATION = 100;
@Nullable
private OrientationHelper mHorizontalHelper;
@Nullable
private OrientationHelper mVerticalHelper;
/* renamed from: androidx.recyclerview.widget.PagerSnapHelper$1 reason: invalid class name */
public class AnonymousClass1 extends LinearSmoothScroller {
public AnonymousClass1(Context context) {
super(context);
}
@Override // androidx.recyclerview.widget.LinearSmoothScroller
public float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {
return 100.0f / ((float) displayMetrics.densityDpi);
}
@Override // androidx.recyclerview.widget.LinearSmoothScroller
public int calculateTimeForScrolling(int i) {
return Math.min(100, super.calculateTimeForScrolling(i));
}
@Override // androidx.recyclerview.widget.LinearSmoothScroller, androidx.recyclerview.widget.RecyclerView.SmoothScroller
public void onTargetFound(View view, RecyclerView.State state, RecyclerView.SmoothScroller.Action action) {
PagerSnapHelper pagerSnapHelper = PagerSnapHelper.this;
int[] calculateDistanceToFinalSnap = pagerSnapHelper.calculateDistanceToFinalSnap(pagerSnapHelper.mRecyclerView.getLayoutManager(), view);
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);
}
}
}
private int distanceToCenter(@NonNull RecyclerView.LayoutManager layoutManager, @NonNull View view, OrientationHelper orientationHelper) {
return ((orientationHelper.getDecoratedMeasurement(view) / 2) + orientationHelper.getDecoratedStart(view)) - ((orientationHelper.getTotalSpace() / 2) + orientationHelper.getStartAfterPadding());
}
@Nullable
private View findCenterView(RecyclerView.LayoutManager layoutManager, OrientationHelper orientationHelper) {
int childCount = layoutManager.getChildCount();
View view = null;
if (childCount == 0) {
return null;
}
int totalSpace = (orientationHelper.getTotalSpace() / 2) + orientationHelper.getStartAfterPadding();
int i = Integer.MAX_VALUE;
for (int i2 = 0; i2 < childCount; i2++) {
View childAt = layoutManager.getChildAt(i2);
int abs = Math.abs(((orientationHelper.getDecoratedMeasurement(childAt) / 2) + orientationHelper.getDecoratedStart(childAt)) - totalSpace);
if (abs < i) {
view = childAt;
i = abs;
}
}
return view;
}
@NonNull
private OrientationHelper getHorizontalHelper(@NonNull RecyclerView.LayoutManager layoutManager) {
OrientationHelper orientationHelper = this.mHorizontalHelper;
if (orientationHelper == null || orientationHelper.mLayoutManager != layoutManager) {
this.mHorizontalHelper = OrientationHelper.createHorizontalHelper(layoutManager);
}
return this.mHorizontalHelper;
}
@Nullable
private OrientationHelper getOrientationHelper(RecyclerView.LayoutManager layoutManager) {
if (layoutManager.canScrollVertically()) {
return getVerticalHelper(layoutManager);
}
if (layoutManager.canScrollHorizontally()) {
return getHorizontalHelper(layoutManager);
}
return null;
}
@NonNull
private OrientationHelper getVerticalHelper(@NonNull RecyclerView.LayoutManager layoutManager) {
OrientationHelper orientationHelper = this.mVerticalHelper;
if (orientationHelper == null || orientationHelper.mLayoutManager != layoutManager) {
this.mVerticalHelper = OrientationHelper.createVerticalHelper(layoutManager);
}
return this.mVerticalHelper;
}
private boolean isForwardFling(RecyclerView.LayoutManager layoutManager, int i, int i2) {
return layoutManager.canScrollHorizontally() ? i > 0 : i2 > 0;
}
private boolean isReverseLayout(RecyclerView.LayoutManager layoutManager) {
PointF computeScrollVectorForPosition;
int itemCount = layoutManager.getItemCount();
if (!(layoutManager instanceof RecyclerView.SmoothScroller.ScrollVectorProvider) || (computeScrollVectorForPosition = ((RecyclerView.SmoothScroller.ScrollVectorProvider) layoutManager).computeScrollVectorForPosition(itemCount - 1)) == null) {
return false;
}
return computeScrollVectorForPosition.x < 0.0f || computeScrollVectorForPosition.y < 0.0f;
}
@Override // androidx.recyclerview.widget.SnapHelper
@Nullable
public int[] calculateDistanceToFinalSnap(@NonNull RecyclerView.LayoutManager layoutManager, @NonNull View view) {
int[] iArr = new int[2];
if (layoutManager.canScrollHorizontally()) {
iArr[0] = distanceToCenter(layoutManager, view, getHorizontalHelper(layoutManager));
} else {
iArr[0] = 0;
}
if (layoutManager.canScrollVertically()) {
iArr[1] = distanceToCenter(layoutManager, view, getVerticalHelper(layoutManager));
} else {
iArr[1] = 0;
}
return iArr;
}
@Override // androidx.recyclerview.widget.SnapHelper
public LinearSmoothScroller createSnapScroller(RecyclerView.LayoutManager layoutManager) {
if (!(layoutManager instanceof RecyclerView.SmoothScroller.ScrollVectorProvider)) {
return null;
}
return new AnonymousClass1(this.mRecyclerView.getContext());
}
@Override // androidx.recyclerview.widget.SnapHelper
@Nullable
public View findSnapView(RecyclerView.LayoutManager layoutManager) {
if (layoutManager.canScrollVertically()) {
return findCenterView(layoutManager, getVerticalHelper(layoutManager));
}
if (layoutManager.canScrollHorizontally()) {
return findCenterView(layoutManager, getHorizontalHelper(layoutManager));
}
return null;
}
@Override // androidx.recyclerview.widget.SnapHelper
public int findTargetSnapPosition(RecyclerView.LayoutManager layoutManager, int i, int i2) {
OrientationHelper orientationHelper;
int itemCount = layoutManager.getItemCount();
if (itemCount == 0 || (orientationHelper = getOrientationHelper(layoutManager)) == null) {
return -1;
}
int i3 = Integer.MIN_VALUE;
int i4 = Integer.MAX_VALUE;
int childCount = layoutManager.getChildCount();
View view = null;
View view2 = null;
for (int i5 = 0; i5 < childCount; i5++) {
View childAt = layoutManager.getChildAt(i5);
if (childAt != null) {
int distanceToCenter = distanceToCenter(layoutManager, childAt, orientationHelper);
if (distanceToCenter <= 0 && distanceToCenter > i3) {
view2 = childAt;
i3 = distanceToCenter;
}
if (distanceToCenter >= 0 && distanceToCenter < i4) {
view = childAt;
i4 = distanceToCenter;
}
}
}
boolean isForwardFling = isForwardFling(layoutManager, i, i2);
if (isForwardFling && view != null) {
return layoutManager.getPosition(view);
}
if (!(isForwardFling || view2 == null)) {
return layoutManager.getPosition(view2);
}
if (isForwardFling) {
view = view2;
}
if (view == null) {
return -1;
}
int position = layoutManager.getPosition(view) + (isReverseLayout(layoutManager) == isForwardFling ? -1 : 1);
if (position < 0 || position >= itemCount) {
return -1;
}
return position;
}
}