content: package androidx.recyclerview.widget;
  
  import androidx.core.os.TraceCompat;
  import androidx.recyclerview.widget.RecyclerView;
  import java.util.ArrayList;
  import java.util.Arrays;
  import java.util.Collections;
  import java.util.Comparator;
  import java.util.concurrent.TimeUnit;
  
  final class GapWorker implements Runnable {
      static final ThreadLocal<GapWorker> sGapWorker = new ThreadLocal<>();
      static Comparator<Task> sTaskComparator = new Comparator<Task>() {
          public int compare(Task lhs, Task rhs) {
              if ((lhs.view == null) != (rhs.view == null)) {
                  return lhs.view == null ? 1 : -1;
              }
              if (lhs.immediate != rhs.immediate) {
                  return lhs.immediate ? -1 : 1;
              }
              int i = rhs.viewVelocity - lhs.viewVelocity;
              if (i != 0) {
                  return i;
              }
              int i2 = lhs.distanceToItem - rhs.distanceToItem;
              if (i2 != 0) {
                  return i2;
              }
              return 0;
          }
      };
      long mFrameIntervalNs;
      long mPostTimeNs;
      ArrayList<RecyclerView> mRecyclerViews = new ArrayList<>();
      private ArrayList<Task> mTasks = new ArrayList<>();
  
      static class LayoutPrefetchRegistryImpl implements RecyclerView.LayoutManager.LayoutPrefetchRegistry {
          int mCount;
          int[] mPrefetchArray;
          int mPrefetchDx;
          int mPrefetchDy;
  
          LayoutPrefetchRegistryImpl() {
          }
  
          public void addPosition(int layoutPosition, int pixelDistance) {
              if (layoutPosition < 0) {
                  throw new IllegalArgumentException("Layout positions must be non-negative");
              } else if (pixelDistance >= 0) {
                  int i = this.mCount * 2;
                  int[] iArr = this.mPrefetchArray;
                  if (iArr == null) {
                      int[] iArr2 = new int[4];
                      this.mPrefetchArray = iArr2;
                      Arrays.fill(iArr2, -1);
                  } else if (i >= iArr.length) {
                      int[] iArr3 = this.mPrefetchArray;
                      int[] iArr4 = new int[(i * 2)];
                      this.mPrefetchArray = iArr4;
                      System.arraycopy(iArr3, 0, iArr4, 0, iArr3.length);
                  }
                  int[] iArr5 = this.mPrefetchArray;
                  iArr5[i] = layoutPosition;
                  iArr5[i + 1] = pixelDistance;
                  this.mCount++;
              } else {
                  throw new IllegalArgumentException("Pixel distance must be non-negative");
              }
          }
  
          /* access modifiers changed from: package-private */
          public void clearPrefetchPositions() {
              int[] iArr = this.mPrefetchArray;
              if (iArr != null) {
                  Arrays.fill(iArr, -1);
              }
              this.mCount = 0;
          }
  
          /* access modifiers changed from: package-private */
          public void collectPrefetchPositionsFromView(RecyclerView view, boolean nested) {
              this.mCount = 0;
              int[] iArr = this.mPrefetchArray;
              if (iArr != null) {
                  Arrays.fill(iArr, -1);
              }
              RecyclerView.LayoutManager layoutManager = view.mLayout;
              if (view.mAdapter != null && layoutManager != null && layoutManager.isItemPrefetchEnabled()) {
                  if (nested) {
                      if (!view.mAdapterHelper.hasPendingUpdates()) {
                          layoutManager.collectInitialPrefetchPositions(view.mAdapter.getItemCount(), this);
                      }
                  } else if (!view.hasPendingAdapterUpdates()) {
                      layoutManager.collectAdjacentPrefetchPositions(this.mPrefetchDx, this.mPrefetchDy, view.mState, this);
                  }
                  if (this.mCount > layoutManager.mPrefetchMaxCountObserved) {
                      layoutManager.mPrefetchMaxCountObserved = this.mCount;
                      layoutManager.mPrefetchMaxObservedInInitialPrefetch = nested;
                      view.mRecycler.updateViewCacheSize();
                  }
              }
          }
  
          /* access modifiers changed from: package-private */
          public boolean lastPrefetchIncludedPosition(int position) {
              if (this.mPrefetchArray == null) {
                  return false;
              }
              int i = this.mCount * 2;
              for (int i2 = 0; i2 < i; i2 += 2) {
                  if (this.mPrefetchArray[i2] == position) {
                      return true;
                  }
              }
              return false;
          }
  
          /* access modifiers changed from: package-private */
          public void setPrefetchVector(int dx, int dy) {
              this.mPrefetchDx = dx;
              this.mPrefetchDy = dy;
          }
      }
  
      static class Task {
          public int distanceToItem;
          public boolean immediate;
          public int position;
          public RecyclerView view;
          public int viewVelocity;
  
          Task() {
          }
  
          public void clear() {
              this.immediate = false;
              this.viewVelocity = 0;
              this.distanceToItem = 0;
              this.view = null;
              this.position = 0;
          }
      }
  
      GapWorker() {
      }
  
      private void buildTaskList() {
          Task task;
          int size = this.mRecyclerViews.size();
          int i = 0;
          for (int i2 = 0; i2 < size; i2++) {
              RecyclerView recyclerView = this.mRecyclerViews.get(i2);
              if (recyclerView.getWindowVisibility() == 0) {
                  recyclerView.mPrefetchRegistry.collectPrefetchPositionsFromView(recyclerView, false);
                  i += recyclerView.mPrefetchRegistry.mCount;
              }
          }
          this.mTasks.ensureCapacity(i);
          int i3 = 0;
          for (int i4 = 0; i4 < size; i4++) {
              RecyclerView recyclerView2 = this.mRecyclerViews.get(i4);
              if (recyclerView2.getWindowVisibility() == 0) {
                  LayoutPrefetchRegistryImpl layoutPrefetchRegistryImpl = recyclerView2.mPrefetchRegistry;
                  int abs = Math.abs(layoutPrefetchRegistryImpl.mPrefetchDx) + Math.abs(layoutPrefetchRegistryImpl.mPrefetchDy);
                  for (int i5 = 0; i5 < layoutPrefetchRegistryImpl.mCount * 2; i5 += 2) {
                      if (i3 >= this.mTasks.size()) {
                          task = new Task();
                          this.mTasks.add(task);
                      } else {
                          task = this.mTasks.get(i3);
                      }
                      int i6 = layoutPrefetchRegistryImpl.mPrefetchArray[i5 + 1];
                      task.immediate = i6 <= abs;
                      task.viewVelocity = abs;
                      task.distanceToItem = i6;
                      task.view = recyclerView2;
                      task.position = layoutPrefetchRegistryImpl.mPrefetchArray[i5];
                      i3++;
                  }
              }
          }
          Collections.sort(this.mTasks, sTaskComparator);
      }
  
      private void flushTaskWithDeadline(Task task, long deadlineNs) {
          RecyclerView.ViewHolder prefetchPositionWithDeadline = prefetchPositionWithDeadline(task.view, task.position, task.immediate ? Long.MAX_VALUE : deadlineNs);
          if (prefetchPositionWithDeadline != null && prefetchPositionWithDeadline.mNestedRecyclerView != null && prefetchPositionWithDeadline.isBound() && !prefetchPositionWithDeadline.isInvalid()) {
              prefetchInnerRecyclerViewWithDeadline((RecyclerView) prefetchPositionWithDeadline.mNestedRecyclerView.get(), deadlineNs);
          }
      }
  
      private void flushTasksWithDeadline(long deadlineNs) {
          int i = 0;
          while (i < this.mTasks.size()) {
              Task task = this.mTasks.get(i);
              if (task.view != null) {
                  flushTaskWithDeadline(task, deadlineNs);
                  task.clear();
                  i++;
              } else {
                  return;
              }
          }
      }
  
      static boolean isPrefetchPositionAttached(RecyclerView view, int position) {
          int unfilteredChildCount = view.mChildHelper.getUnfilteredChildCount();
          for (int i = 0; i < unfilteredChildCount; i++) {
              RecyclerView.ViewHolder childViewHolderInt = RecyclerView.getChildViewHolderInt(view.mChildHelper.getUnfilteredChildAt(i));
              if (childViewHolderInt.mPosition == position && !childViewHolderInt.isInvalid()) {
                  return true;
              }
          }
          return false;
      }
  
      private void prefetchInnerRecyclerViewWithDeadline(RecyclerView innerView, long deadlineNs) {
          if (innerView != null) {
              if (innerView.mDataSetHasChangedAfterLayout && innerView.mChildHelper.getUnfilteredChildCount() != 0) {
                  innerView.removeAndRecycleViews();
              }
              LayoutPrefetchRegistryImpl layoutPrefetchRegistryImpl = innerView.mPrefetchRegistry;
              layoutPrefetchRegistryImpl.collectPrefetchPositionsFromView(innerView, true);
              if (layoutPrefetchRegistryImpl.mCount != 0) {
                  try {
                      TraceCompat.beginSection("RV Nested Prefetch");
                      innerView.mState.prepareForNestedPrefetch(innerView.mAdapter);
                      for (int i = 0; i < layoutPrefetchRegistryImpl.mCount * 2; i += 2) {
                          prefetchPositionWithDeadline(innerView, layoutPrefetchRegistryImpl.mPrefetchArray[i], deadlineNs);
                      }
                  } finally {
                      TraceCompat.endSection();
                  }
              }
          }
      }
  
      private RecyclerView.ViewHolder prefetchPositionWithDeadline(RecyclerView view, int position, long deadlineNs) {
          if (isPrefetchPositionAttached(view, position)) {
              return null;
          }
          RecyclerView.Recycler recycler = view.mRecycler;
          try {
              view.onEnterLayoutOrScroll();
              RecyclerView.ViewHolder tryGetViewHolderForPositionByDeadline = recycler.tryGetViewHolderForPositionByDeadline(position, false, deadlineNs);
              if (tryGetViewHolderForPositionByDeadline != null) {
                  if (!tryGetViewHolderForPositionByDeadline.isBound() || tryGetViewHolderForPositionByDeadline.isInvalid()) {
                      recycler.addViewHolderToRecycledViewPool(tryGetViewHolderForPositionByDeadline, false);
                  } else {
                      recycler.recycleView(tryGetViewHolderForPositionByDeadline.itemView);
                  }
              }
              return tryGetViewHolderForPositionByDeadline;
          } finally {
              view.onExitLayoutOrScroll(false);
          }
      }
  
      public void add(RecyclerView recyclerView) {
          this.mRecyclerViews.add(recyclerView);
      }
  
      /* access modifiers changed from: package-private */
      public void postFromTraversal(RecyclerView recyclerView, int prefetchDx, int prefetchDy) {
          if (recyclerView.isAttachedToWindow() && this.mPostTimeNs == 0) {
              this.mPostTimeNs = recyclerView.getNanoTime();
              recyclerView.post(this);
          }
          recyclerView.mPrefetchRegistry.setPrefetchVector(prefetchDx, prefetchDy);
      }
  
      /* access modifiers changed from: package-private */
      public void prefetch(long deadlineNs) {
          buildTaskList();
          flushTasksWithDeadline(deadlineNs);
      }
  
      public void remove(RecyclerView recyclerView) {
          boolean remove = this.mRecyclerViews.remove(recyclerView);
      }
  
      public void run() {
          try {
              TraceCompat.beginSection("RV Prefetch");
              if (!this.mRecyclerViews.isEmpty()) {
                  int size = this.mRecyclerViews.size();
                  long j = 0;
                  for (int i = 0; i < size; i++) {
                      RecyclerView recyclerView = this.mRecyclerViews.get(i);
                      if (recyclerView.getWindowVisibility() == 0) {
                          j = Math.max(recyclerView.getDrawingTime(), j);
                      }
                  }
                  if (j == 0) {
                      this.mPostTimeNs = 0;
                      TraceCompat.endSection();
                      return;
                  }
                  prefetch(TimeUnit.MILLISECONDS.toNanos(j) + this.mFrameIntervalNs);
                  this.mPostTimeNs = 0;
                  TraceCompat.endSection();
              }
          } finally {
              this.mPostTimeNs = 0;
              TraceCompat.endSection();
          }
      }
  }
  