package okhttp3.internal.concurrent;

import com.google.android.recaptcha.internal.a;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.RejectedExecutionException;
import java.util.logging.Level;
import kotlin.jvm.internal.o;
import okhttp3.internal.Util;
import okhttp3.internal.concurrent.TaskRunner;

/* JADX INFO: loaded from: classes3.dex */
public final class TaskQueue {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final TaskRunner f22265a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final String f22266b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public boolean f22267c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public Task f22268d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final ArrayList f22269e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public boolean f22270f;

    public static final class AwaitIdleTask extends Task {

        /* JADX INFO: renamed from: e, reason: collision with root package name */
        public final CountDownLatch f22271e;

        public AwaitIdleTask() {
            super(a.n(new StringBuilder(), Util.f22185g, " awaitIdle"), false);
            this.f22271e = new CountDownLatch(1);
        }

        @Override // okhttp3.internal.concurrent.Task
        public final long a() {
            this.f22271e.countDown();
            return -1L;
        }
    }

    public TaskQueue(TaskRunner taskRunner, String name) {
        o.h(taskRunner, "taskRunner");
        o.h(name, "name");
        this.f22265a = taskRunner;
        this.f22266b = name;
        this.f22269e = new ArrayList();
    }

    public final void a() {
        byte[] bArr = Util.f22179a;
        synchronized (this.f22265a) {
            if (b()) {
                this.f22265a.d(this);
            }
        }
    }

    public final boolean b() {
        Task task = this.f22268d;
        if (task != null && task.f22262b) {
            this.f22270f = true;
        }
        ArrayList arrayList = this.f22269e;
        boolean z6 = false;
        for (int size = arrayList.size() - 1; -1 < size; size--) {
            if (((Task) arrayList.get(size)).f22262b) {
                Task task2 = (Task) arrayList.get(size);
                TaskRunner.f22273h.getClass();
                if (TaskRunner.f22275j.isLoggable(Level.FINE)) {
                    TaskLoggerKt.a(task2, this, "canceled");
                }
                arrayList.remove(size);
                z6 = true;
            }
        }
        return z6;
    }

    public final void c(Task task, long j4) {
        o.h(task, "task");
        synchronized (this.f22265a) {
            if (!this.f22267c) {
                if (e(task, j4, false)) {
                    this.f22265a.d(this);
                }
            } else if (task.f22262b) {
                TaskRunner.f22273h.getClass();
                if (TaskRunner.f22275j.isLoggable(Level.FINE)) {
                    TaskLoggerKt.a(task, this, "schedule canceled (queue is shutdown)");
                }
            } else {
                TaskRunner.f22273h.getClass();
                if (TaskRunner.f22275j.isLoggable(Level.FINE)) {
                    TaskLoggerKt.a(task, this, "schedule failed (queue is shutdown)");
                }
                throw new RejectedExecutionException();
            }
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:18:0x0051  */
    /* JADX WARN: Removed duplicated region for block: B:25:0x0078  */
    /* JADX WARN: Removed duplicated region for block: B:31:0x008c  */
    /* JADX WARN: Removed duplicated region for block: B:34:0x0095 A[RETURN] */
    /* JADX WARN: Removed duplicated region for block: B:39:0x0089 A[SYNTHETIC] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final boolean e(Task task, long j4, boolean z6) {
        Iterator it;
        int size;
        o.h(task, "task");
        TaskQueue taskQueue = task.f22263c;
        if (taskQueue != this) {
            if (taskQueue != null) {
                throw new IllegalStateException("task is in multiple queues");
            }
            task.f22263c = this;
        }
        TaskRunner.RealBackend realBackend = this.f22265a.f22276a;
        long jNanoTime = System.nanoTime();
        long j6 = jNanoTime + j4;
        ArrayList arrayList = this.f22269e;
        int iIndexOf = arrayList.indexOf(task);
        if (iIndexOf == -1) {
            task.f22264d = j6;
            TaskRunner.f22273h.getClass();
            if (TaskRunner.f22275j.isLoggable(Level.FINE)) {
                TaskLoggerKt.a(task, this, z6 ? "run again after ".concat(TaskLoggerKt.b(j6 - jNanoTime)) : "scheduled after ".concat(TaskLoggerKt.b(j6 - jNanoTime)));
            }
            it = arrayList.iterator();
            size = 0;
            while (true) {
                if (it.hasNext()) {
                    size = -1;
                    break;
                }
                if (((Task) it.next()).f22264d - jNanoTime > j4) {
                    break;
                }
                size++;
            }
            if (size == -1) {
                size = arrayList.size();
            }
            arrayList.add(size, task);
            if (size != 0) {
                return true;
            }
        } else if (task.f22264d <= j6) {
            TaskRunner.f22273h.getClass();
            if (TaskRunner.f22275j.isLoggable(Level.FINE)) {
                TaskLoggerKt.a(task, this, "already scheduled");
                return false;
            }
        } else {
            arrayList.remove(iIndexOf);
            task.f22264d = j6;
            TaskRunner.f22273h.getClass();
            if (TaskRunner.f22275j.isLoggable(Level.FINE)) {
            }
            it = arrayList.iterator();
            size = 0;
            while (true) {
                if (it.hasNext()) {
                }
                size++;
            }
            if (size == -1) {
            }
            arrayList.add(size, task);
            if (size != 0) {
            }
        }
        return false;
    }

    public final void f() {
        byte[] bArr = Util.f22179a;
        synchronized (this.f22265a) {
            this.f22267c = true;
            if (b()) {
                this.f22265a.d(this);
            }
        }
    }

    public final String toString() {
        return this.f22266b;
    }
}
