package N1;

import android.os.Trace;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.Collections;
import java.util.concurrent.TimeUnit;

/* JADX INFO: loaded from: classes.dex */
public final class B implements Runnable {

    /* JADX INFO: renamed from: u, reason: collision with root package name */
    public static final ThreadLocal f4410u = new ThreadLocal();

    /* JADX INFO: renamed from: v, reason: collision with root package name */
    public static final C0215p f4411v = new C0215p(1);

    /* JADX INFO: renamed from: r, reason: collision with root package name */
    public long f4413r;

    /* JADX INFO: renamed from: s, reason: collision with root package name */
    public long f4414s;

    /* JADX INFO: renamed from: q, reason: collision with root package name */
    public final ArrayList f4412q = new ArrayList();

    /* JADX INFO: renamed from: t, reason: collision with root package name */
    public final ArrayList f4415t = new ArrayList();

    public static t0 c(RecyclerView recyclerView, int i, long j8) {
        int iL = recyclerView.f9080v.l();
        for (int i5 = 0; i5 < iL; i5++) {
            t0 t0VarN = RecyclerView.N(recyclerView.f9080v.k(i5));
            if (t0VarN.f4656c == i && !t0VarN.h()) {
                return null;
            }
        }
        i0 i0Var = recyclerView.f9074s;
        if (j8 == Long.MAX_VALUE) {
            try {
                if (I.g.a()) {
                    Trace.beginSection("RV Prefetch forced - needed next frame");
                }
            } catch (Throwable th) {
                recyclerView.W(false);
                Trace.endSection();
                throw th;
            }
        }
        recyclerView.V();
        t0 t0VarL = i0Var.l(j8, i);
        if (t0VarL != null) {
            if (!t0VarL.g() || t0VarL.h()) {
                i0Var.a(t0VarL, false);
            } else {
                i0Var.i(t0VarL.f4654a);
            }
        }
        recyclerView.W(false);
        Trace.endSection();
        return t0VarL;
    }

    public final void a(RecyclerView recyclerView, int i, int i5) {
        if (recyclerView.f9031J) {
            if (RecyclerView.f9008U0 && !this.f4412q.contains(recyclerView)) {
                throw new IllegalStateException("attempting to post unregistered view!");
            }
            if (this.f4413r == 0) {
                this.f4413r = recyclerView.getNanoTime();
                recyclerView.post(this);
            }
        }
        C0224z c0224z = recyclerView.f9085x0;
        c0224z.f4726b = i;
        c0224z.f4727c = i5;
    }

    /* JADX WARN: Removed duplicated region for block: B:46:0x00ce  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void b(long j8) {
        A a3;
        RecyclerView recyclerView;
        RecyclerView recyclerView2;
        A a8;
        ArrayList arrayList = this.f4412q;
        int size = arrayList.size();
        int i = 0;
        int i5 = 0;
        for (int i8 = 0; i8 < size; i8++) {
            RecyclerView recyclerView3 = (RecyclerView) arrayList.get(i8);
            if (recyclerView3.getWindowVisibility() == 0) {
                C0224z c0224z = recyclerView3.f9085x0;
                c0224z.c(recyclerView3, false);
                i5 += c0224z.f4728d;
            }
        }
        ArrayList arrayList2 = this.f4415t;
        arrayList2.ensureCapacity(i5);
        int i9 = 0;
        int i10 = 0;
        while (i9 < size) {
            RecyclerView recyclerView4 = (RecyclerView) arrayList.get(i9);
            if (recyclerView4.getWindowVisibility() == 0) {
                C0224z c0224z2 = recyclerView4.f9085x0;
                int iAbs = Math.abs(c0224z2.f4727c) + Math.abs(c0224z2.f4726b);
                for (int i11 = i; i11 < c0224z2.f4728d * 2; i11 += 2) {
                    if (i10 >= arrayList2.size()) {
                        a8 = new A();
                        arrayList2.add(a8);
                    } else {
                        a8 = (A) arrayList2.get(i10);
                    }
                    int[] iArr = c0224z2.f4725a;
                    int i12 = iArr[i11 + 1];
                    a8.f4395a = i12 <= iAbs;
                    a8.f4396b = iAbs;
                    a8.f4397c = i12;
                    a8.f4398d = recyclerView4;
                    a8.f4399e = iArr[i11];
                    i10++;
                }
            }
            i9++;
            i = 0;
        }
        Collections.sort(arrayList2, f4411v);
        for (int i13 = 0; i13 < arrayList2.size() && (recyclerView = (a3 = (A) arrayList2.get(i13)).f4398d) != null; i13++) {
            t0 t0VarC = c(recyclerView, a3.f4399e, a3.f4395a ? Long.MAX_VALUE : j8);
            if (t0VarC != null && t0VarC.f4655b != null && t0VarC.g() && !t0VarC.h() && (recyclerView2 = (RecyclerView) t0VarC.f4655b.get()) != null) {
                if (recyclerView2.f9052U && recyclerView2.f9080v.l() != 0) {
                    Z z8 = recyclerView2.f9061g0;
                    if (z8 != null) {
                        z8.e();
                    }
                    androidx.recyclerview.widget.a aVar = recyclerView2.D;
                    i0 i0Var = recyclerView2.f9074s;
                    if (aVar != null) {
                        aVar.G0(i0Var);
                        recyclerView2.D.H0(i0Var);
                    }
                    i0Var.f4555a.clear();
                    i0Var.g();
                }
                C0224z c0224z3 = recyclerView2.f9085x0;
                c0224z3.c(recyclerView2, true);
                if (c0224z3.f4728d != 0) {
                    try {
                        Trace.beginSection(j8 == Long.MAX_VALUE ? "RV Nested Prefetch" : "RV Nested Prefetch forced - needed next frame");
                        p0 p0Var = recyclerView2.f9087y0;
                        T t6 = recyclerView2.f9019C;
                        p0Var.f4617d = 1;
                        p0Var.f4618e = t6.a();
                        p0Var.f4620g = false;
                        p0Var.f4621h = false;
                        p0Var.i = false;
                        for (int i14 = 0; i14 < c0224z3.f4728d * 2; i14 += 2) {
                            c(recyclerView2, c0224z3.f4725a[i14], j8);
                        }
                        Trace.endSection();
                    } catch (Throwable th) {
                        Trace.endSection();
                        throw th;
                    }
                }
            }
            a3.f4395a = false;
            a3.f4396b = 0;
            a3.f4397c = 0;
            a3.f4398d = null;
            a3.f4399e = 0;
        }
    }

    @Override // java.lang.Runnable
    public final void run() {
        try {
            Trace.beginSection("RV Prefetch");
            ArrayList arrayList = this.f4412q;
            if (!arrayList.isEmpty()) {
                int size = arrayList.size();
                long jMax = 0;
                for (int i = 0; i < size; i++) {
                    RecyclerView recyclerView = (RecyclerView) arrayList.get(i);
                    if (recyclerView.getWindowVisibility() == 0) {
                        jMax = Math.max(recyclerView.getDrawingTime(), jMax);
                    }
                }
                if (jMax != 0) {
                    b(TimeUnit.MILLISECONDS.toNanos(jMax) + this.f4414s);
                }
            }
        } finally {
            this.f4413r = 0L;
            Trace.endSection();
        }
    }
}
