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; /* loaded from: classes.dex */ final class GapWorker implements Runnable { static final ThreadLocal e = new ThreadLocal<>(); static Comparator f = new Comparator() { // from class: androidx.recyclerview.widget.GapWorker.1 @Override // java.util.Comparator /* renamed from: a, reason: merged with bridge method [inline-methods] */ public int compare(Task task, Task task2) { if ((task.d == null) != (task2.d == null)) { return task.d == null ? 1 : -1; } boolean z = task.a; if (z != task2.a) { return z ? -1 : 1; } int i = task2.b - task.b; if (i != 0) { return i; } int i2 = task.c - task2.c; if (i2 != 0) { return i2; } return 0; } }; long b; long c; ArrayList a = new ArrayList<>(); private ArrayList d = new ArrayList<>(); static class Task { public boolean a; public int b; public int c; public RecyclerView d; public int e; Task() { } public void a() { this.a = false; this.b = 0; this.c = 0; this.d = null; this.e = 0; } } GapWorker() { } public void a(RecyclerView recyclerView) { this.a.add(recyclerView); } public void b(RecyclerView recyclerView) { this.a.remove(recyclerView); } @Override // java.lang.Runnable public void run() { try { TraceCompat.a("RV Prefetch"); if (!this.a.isEmpty()) { int size = this.a.size(); long j = 0; for (int i = 0; i < size; i++) { RecyclerView recyclerView = this.a.get(i); if (recyclerView.getWindowVisibility() == 0) { j = Math.max(recyclerView.getDrawingTime(), j); } } if (j != 0) { a(TimeUnit.MILLISECONDS.toNanos(j) + this.c); } } } finally { this.b = 0L; TraceCompat.a(); } } private void b(long j) { for (int i = 0; i < this.d.size(); i++) { Task task = this.d.get(i); if (task.d == null) { return; } a(task, j); task.a(); } } void a(RecyclerView recyclerView, int i, int i2) { if (recyclerView.isAttachedToWindow() && this.b == 0) { this.b = recyclerView.getNanoTime(); recyclerView.post(this); } recyclerView.h0.b(i, i2); } private void a() { Task task; int size = this.a.size(); int i = 0; for (int i2 = 0; i2 < size; i2++) { RecyclerView recyclerView = this.a.get(i2); if (recyclerView.getWindowVisibility() == 0) { recyclerView.h0.a(recyclerView, false); i += recyclerView.h0.d; } } this.d.ensureCapacity(i); int i3 = 0; for (int i4 = 0; i4 < size; i4++) { RecyclerView recyclerView2 = this.a.get(i4); if (recyclerView2.getWindowVisibility() == 0) { LayoutPrefetchRegistryImpl layoutPrefetchRegistryImpl = recyclerView2.h0; int abs = Math.abs(layoutPrefetchRegistryImpl.a) + Math.abs(layoutPrefetchRegistryImpl.b); int i5 = i3; for (int i6 = 0; i6 < layoutPrefetchRegistryImpl.d * 2; i6 += 2) { if (i5 >= this.d.size()) { task = new Task(); this.d.add(task); } else { task = this.d.get(i5); } int i7 = layoutPrefetchRegistryImpl.c[i6 + 1]; task.a = i7 <= abs; task.b = abs; task.c = i7; task.d = recyclerView2; task.e = layoutPrefetchRegistryImpl.c[i6]; i5++; } i3 = i5; } } Collections.sort(this.d, f); } static class LayoutPrefetchRegistryImpl implements RecyclerView.LayoutManager.LayoutPrefetchRegistry { int a; int b; int[] c; int d; LayoutPrefetchRegistryImpl() { } void a(RecyclerView recyclerView, boolean z) { this.d = 0; int[] iArr = this.c; if (iArr != null) { Arrays.fill(iArr, -1); } RecyclerView.LayoutManager layoutManager = recyclerView.m; if (recyclerView.l == null || layoutManager == null || !layoutManager.w()) { return; } if (z) { if (!recyclerView.d.c()) { layoutManager.a(recyclerView.l.getItemCount(), this); } } else if (!recyclerView.j()) { layoutManager.a(this.a, this.b, recyclerView.i0, this); } int i = this.d; if (i > layoutManager.m) { layoutManager.m = i; layoutManager.n = z; recyclerView.b.j(); } } void b(int i, int i2) { this.a = i; this.b = i2; } @Override // androidx.recyclerview.widget.RecyclerView.LayoutManager.LayoutPrefetchRegistry public void a(int i, int i2) { if (i < 0) { throw new IllegalArgumentException("Layout positions must be non-negative"); } if (i2 >= 0) { int i3 = this.d * 2; int[] iArr = this.c; if (iArr == null) { this.c = new int[4]; Arrays.fill(this.c, -1); } else if (i3 >= iArr.length) { this.c = new int[i3 * 2]; System.arraycopy(iArr, 0, this.c, 0, iArr.length); } int[] iArr2 = this.c; iArr2[i3] = i; iArr2[i3 + 1] = i2; this.d++; return; } throw new IllegalArgumentException("Pixel distance must be non-negative"); } boolean a(int i) { if (this.c != null) { int i2 = this.d * 2; for (int i3 = 0; i3 < i2; i3 += 2) { if (this.c[i3] == i) { return true; } } } return false; } void a() { int[] iArr = this.c; if (iArr != null) { Arrays.fill(iArr, -1); } this.d = 0; } } static boolean a(RecyclerView recyclerView, int i) { int b = recyclerView.e.b(); for (int i2 = 0; i2 < b; i2++) { RecyclerView.ViewHolder m = RecyclerView.m(recyclerView.e.d(i2)); if (m.mPosition == i && !m.isInvalid()) { return true; } } return false; } private RecyclerView.ViewHolder a(RecyclerView recyclerView, int i, long j) { if (a(recyclerView, i)) { return null; } RecyclerView.Recycler recycler = recyclerView.b; try { recyclerView.q(); RecyclerView.ViewHolder a = recycler.a(i, false, j); if (a != null) { if (a.isBound() && !a.isInvalid()) { recycler.b(a.itemView); } else { recycler.a(a, false); } } return a; } finally { recyclerView.a(false); } } private void a(RecyclerView recyclerView, long j) { if (recyclerView == null) { return; } if (recyclerView.E && recyclerView.e.b() != 0) { recyclerView.t(); } LayoutPrefetchRegistryImpl layoutPrefetchRegistryImpl = recyclerView.h0; layoutPrefetchRegistryImpl.a(recyclerView, true); if (layoutPrefetchRegistryImpl.d != 0) { try { TraceCompat.a("RV Nested Prefetch"); recyclerView.i0.a(recyclerView.l); for (int i = 0; i < layoutPrefetchRegistryImpl.d * 2; i += 2) { a(recyclerView, layoutPrefetchRegistryImpl.c[i], j); } } finally { TraceCompat.a(); } } } private void a(Task task, long j) { RecyclerView.ViewHolder a = a(task.d, task.e, task.a ? Long.MAX_VALUE : j); if (a == null || a.mNestedRecyclerView == null || !a.isBound() || a.isInvalid()) { return; } a(a.mNestedRecyclerView.get(), j); } void a(long j) { a(); b(j); } }