package ῲ.䀟.K;

import android.annotation.SuppressLint;
import android.os.Build;
import android.os.Trace;
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;
import ῲ.ᨨ.ඓ.C1724;

/* renamed from: ῲ.䀟.K.ᨨ  reason: contains not printable characters */
/* compiled from: GapWorker */
public final class RunnableC2312 implements Runnable {

    /* renamed from: ⁱ  reason: contains not printable characters */
    public static Comparator<C2313> f7984 = new C2315();

    /* renamed from: 㴹  reason: contains not printable characters */
    public static final ThreadLocal<RunnableC2312> f7985 = new ThreadLocal<>();

    /* renamed from: ʬ  reason: contains not printable characters */
    public long f7986;

    /* renamed from: ඓ  reason: contains not printable characters */
    public ArrayList<C2313> f7987 = new ArrayList<>();

    /* renamed from: ῲ  reason: contains not printable characters */
    public ArrayList<RecyclerView> f7988 = new ArrayList<>();

    /* renamed from: 㸾  reason: contains not printable characters */
    public long f7989;

    /* renamed from: ῲ.䀟.K.ᨨ$ʬ  reason: contains not printable characters */
    /* compiled from: GapWorker */
    public static class C2313 {

        /* renamed from: ʬ  reason: contains not printable characters */
        public int f7990;

        /* renamed from: ඓ  reason: contains not printable characters */
        public int f7991;

        /* renamed from: ῲ  reason: contains not printable characters */
        public int f7992;

        /* renamed from: K  reason: contains not printable characters */
        public boolean f7993;

        /* renamed from: 㸾  reason: contains not printable characters */
        public RecyclerView f7994;
    }

    /* renamed from: ῲ.䀟.K.ᨨ$K  reason: contains not printable characters */
    /* compiled from: GapWorker */
    public static class C2315 implements Comparator<C2313> {
        /* JADX DEBUG: Method arguments types fixed to match base method, original types: [java.lang.Object, java.lang.Object] */
        /* JADX WARNING: Removed duplicated region for block: B:13:0x001d A[RETURN, SYNTHETIC] */
        @Override // java.util.Comparator
        public int compare(C2313 r6, C2313 r7) {
            C2313 r62 = r6;
            C2313 r72 = r7;
            if ((r62.f7994 == null) != (r72.f7994 == null)) {
                return r62.f7994 == null ? 1 : -1;
            }
            boolean z = r62.f7993;
            if (z == r72.f7993) {
                int i = r72.f7992 - r62.f7992;
                if (i != 0) {
                    return i;
                }
                int i2 = r62.f7990 - r72.f7990;
                if (i2 != 0) {
                    return i2;
                }
                return 0;
            } else if (z) {
            }
        }
    }

    public void run() {
        try {
            C1724.m4432(RecyclerView.TRACE_PREFETCH_TAG);
            if (this.f7988.isEmpty()) {
                this.f7986 = 0;
                int i = Build.VERSION.SDK_INT;
                Trace.endSection();
                return;
            }
            int size = this.f7988.size();
            long j = 0;
            for (int i2 = 0; i2 < size; i2++) {
                RecyclerView recyclerView = this.f7988.get(i2);
                if (recyclerView.getWindowVisibility() == 0) {
                    j = Math.max(recyclerView.getDrawingTime(), j);
                }
            }
            if (j == 0) {
                this.f7986 = 0;
                int i3 = Build.VERSION.SDK_INT;
                Trace.endSection();
                return;
            }
            m6041(TimeUnit.MILLISECONDS.toNanos(j) + this.f7989);
            this.f7986 = 0;
            int i4 = Build.VERSION.SDK_INT;
            Trace.endSection();
        } catch (Throwable th) {
            this.f7986 = 0;
            C1724.m4431();
            throw th;
        }
    }

    /* renamed from: K  reason: contains not printable characters */
    public void m6042(RecyclerView recyclerView, int i, int i2) {
        if (recyclerView.isAttachedToWindow() && this.f7986 == 0) {
            this.f7986 = recyclerView.getNanoTime();
            recyclerView.post(this);
        }
        C2314 r6 = recyclerView.mPrefetchRegistry;
        r6.f7997 = i;
        r6.f7996 = i2;
    }

    /* renamed from: K  reason: contains not printable characters */
    public void m6041(long j) {
        RecyclerView recyclerView;
        C2313 r9;
        int size = this.f7988.size();
        int i = 0;
        for (int i2 = 0; i2 < size; i2++) {
            RecyclerView recyclerView2 = this.f7988.get(i2);
            if (recyclerView2.getWindowVisibility() == 0) {
                recyclerView2.mPrefetchRegistry.m6044(recyclerView2, false);
                i += recyclerView2.mPrefetchRegistry.f7998;
            }
        }
        this.f7987.ensureCapacity(i);
        int i3 = 0;
        for (int i4 = 0; i4 < size; i4++) {
            RecyclerView recyclerView3 = this.f7988.get(i4);
            if (recyclerView3.getWindowVisibility() == 0) {
                C2314 r6 = recyclerView3.mPrefetchRegistry;
                int abs = Math.abs(r6.f7996) + Math.abs(r6.f7997);
                int i5 = i3;
                for (int i6 = 0; i6 < r6.f7998 * 2; i6 += 2) {
                    if (i5 >= this.f7987.size()) {
                        r9 = new C2313();
                        this.f7987.add(r9);
                    } else {
                        r9 = this.f7987.get(i5);
                    }
                    int i7 = r6.f7995[i6 + 1];
                    r9.f7993 = i7 <= abs;
                    r9.f7992 = abs;
                    r9.f7990 = i7;
                    r9.f7994 = recyclerView3;
                    r9.f7991 = r6.f7995[i6];
                    i5++;
                }
                i3 = i5;
            }
        }
        Collections.sort(this.f7987, f7984);
        for (int i8 = 0; i8 < this.f7987.size(); i8++) {
            C2313 r2 = this.f7987.get(i8);
            if (r2.f7994 != null) {
                RecyclerView.AbstractC0269 r3 = m6040(r2.f7994, r2.f7991, r2.f7993 ? RecyclerView.FOREVER_NS : j);
                if (!(r3 == null || r3.mNestedRecyclerView == null || !r3.isBound() || r3.isInvalid() || (recyclerView = r3.mNestedRecyclerView.get()) == null)) {
                    if (recyclerView.mDataSetHasChangedAfterLayout && recyclerView.mChildHelper.m6049() != 0) {
                        recyclerView.removeAndRecycleViews();
                    }
                    C2314 r5 = recyclerView.mPrefetchRegistry;
                    r5.m6044(recyclerView, true);
                    if (r5.f7998 != 0) {
                        try {
                            C1724.m4432(RecyclerView.TRACE_NESTED_PREFETCH_TAG);
                            RecyclerView.C0248 r62 = recyclerView.mState;
                            RecyclerView.AbstractC0246 r7 = recyclerView.mAdapter;
                            r62.f1498 = 1;
                            r62.f1508 = r7.getItemCount();
                            r62.f1510 = false;
                            r62.f1501 = false;
                            r62.f1507 = false;
                            for (int i9 = 0; i9 < r5.f7998 * 2; i9 += 2) {
                                m6040(recyclerView, r5.f7995[i9], j);
                            }
                            int i10 = Build.VERSION.SDK_INT;
                            Trace.endSection();
                        } catch (Throwable th) {
                            C1724.m4431();
                            throw th;
                        }
                    }
                }
                r2.f7993 = false;
                r2.f7992 = 0;
                r2.f7990 = 0;
                r2.f7994 = null;
                r2.f7991 = 0;
            } else {
                return;
            }
        }
    }

    @SuppressLint({"VisibleForTests"})
    /* renamed from: ῲ.䀟.K.ᨨ$ῲ  reason: contains not printable characters */
    /* compiled from: GapWorker */
    public static class C2314 implements RecyclerView.AbstractC0254.AbstractC0255 {

        /* renamed from: ʬ  reason: contains not printable characters */
        public int[] f7995;

        /* renamed from: ῲ  reason: contains not printable characters */
        public int f7996;

        /* renamed from: K  reason: contains not printable characters */
        public int f7997;

        /* renamed from: 㸾  reason: contains not printable characters */
        public int f7998;

        /* renamed from: K  reason: contains not printable characters */
        public void m6044(RecyclerView recyclerView, boolean z) {
            this.f7998 = 0;
            int[] iArr = this.f7995;
            if (iArr != null) {
                Arrays.fill(iArr, -1);
            }
            RecyclerView.AbstractC0254 r0 = recyclerView.mLayout;
            if (recyclerView.mAdapter != null && r0 != null && r0.isItemPrefetchEnabled()) {
                if (z) {
                    if (!recyclerView.mAdapterHelper.m6080()) {
                        r0.collectInitialPrefetchPositions(recyclerView.mAdapter.getItemCount(), this);
                    }
                } else if (!recyclerView.hasPendingAdapterUpdates()) {
                    r0.collectAdjacentPrefetchPositions(this.f7997, this.f7996, recyclerView.mState, this);
                }
                int i = this.f7998;
                if (i > r0.mPrefetchMaxCountObserved) {
                    r0.mPrefetchMaxCountObserved = i;
                    r0.mPrefetchMaxObservedInInitialPrefetch = z;
                    recyclerView.mRecycler.m871();
                }
            }
        }

        /* renamed from: K  reason: contains not printable characters */
        public void m6043(int i, int i2) {
            if (i < 0) {
                throw new IllegalArgumentException("Layout positions must be non-negative");
            } else if (i2 >= 0) {
                int i3 = this.f7998 * 2;
                int[] iArr = this.f7995;
                if (iArr == null) {
                    this.f7995 = new int[4];
                    Arrays.fill(this.f7995, -1);
                } else if (i3 >= iArr.length) {
                    this.f7995 = new int[(i3 * 2)];
                    System.arraycopy(iArr, 0, this.f7995, 0, iArr.length);
                }
                int[] iArr2 = this.f7995;
                iArr2[i3] = i;
                iArr2[i3 + 1] = i2;
                this.f7998++;
            } else {
                throw new IllegalArgumentException("Pixel distance must be non-negative");
            }
        }

        /* renamed from: K  reason: contains not printable characters */
        public boolean m6045(int i) {
            if (this.f7995 != null) {
                int i2 = this.f7998 * 2;
                for (int i3 = 0; i3 < i2; i3 += 2) {
                    if (this.f7995[i3] == i) {
                        return true;
                    }
                }
            }
            return false;
        }
    }

    /* renamed from: K  reason: contains not printable characters */
    public final RecyclerView.AbstractC0269 m6040(RecyclerView recyclerView, int i, long j) {
        boolean z;
        int r0 = recyclerView.mChildHelper.m6049();
        int i2 = 0;
        while (true) {
            if (i2 >= r0) {
                z = false;
                break;
            }
            RecyclerView.AbstractC0269 childViewHolderInt = RecyclerView.getChildViewHolderInt(recyclerView.mChildHelper.m6057(i2));
            if (childViewHolderInt.mPosition == i && !childViewHolderInt.isInvalid()) {
                z = true;
                break;
            }
            i2++;
        }
        if (z) {
            return null;
        }
        RecyclerView.C0264 r02 = recyclerView.mRecycler;
        try {
            recyclerView.onEnterLayoutOrScroll();
            RecyclerView.AbstractC0269 r7 = r02.m864(i, false, j);
            if (r7 != null) {
                if (!r7.isBound() || r7.isInvalid()) {
                    r02.m870(r7, false);
                } else {
                    r02.m861(r7.itemView);
                }
            }
            return r7;
        } finally {
            recyclerView.onExitLayoutOrScroll(false);
        }
    }
}
