package okhttp3.n0.concurrent;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.RejectedExecutionException;
import java.util.logging.Level;
import kotlin.Metadata;
import kotlin.jvm.internal.o;
import okhttp3.n0.c;
import okhttp3.n0.concurrent.TaskRunner;

/* JADX INFO: renamed from: y.n0.f.c, reason: from Kotlin metadata */
/* JADX INFO: loaded from: classes2.dex */
@Metadata(d1 = {"\u0000V\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\u000e\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0002\b\u0005\n\u0002\u0010\u000b\n\u0002\b\u0005\n\u0002\u0010!\n\u0002\b\u0005\n\u0002\u0010 \n\u0002\b\u0007\n\u0002\u0010\u0002\n\u0002\b\u0004\n\u0002\u0010\t\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\b\u0018\u00002\u00020\u0001:\u00013B\u0017\b\u0000\u0012\u0006\u0010\u0002\u001a\u00020\u0003\u0012\u0006\u0010\u0004\u001a\u00020\u0005¢\u0006\u0002\u0010\u0006J\u0006\u0010!\u001a\u00020\"J\r\u0010#\u001a\u00020\u000eH\u0000¢\u0006\u0002\b$J8\u0010%\u001a\u00020\"2\u0006\u0010\u0004\u001a\u00020\u00052\b\b\u0002\u0010&\u001a\u00020'2\b\b\u0002\u0010(\u001a\u00020\u000e2\u000e\b\u0004\u0010)\u001a\b\u0012\u0004\u0012\u00020\"0*H\u0086\bø\u0001\u0000J\u0006\u0010+\u001a\u00020,J.\u0010-\u001a\u00020\"2\u0006\u0010\u0004\u001a\u00020\u00052\b\b\u0002\u0010&\u001a\u00020'2\u000e\b\u0004\u0010)\u001a\b\u0012\u0004\u0012\u00020'0*H\u0086\bø\u0001\u0000J\u0018\u0010-\u001a\u00020\"2\u0006\u0010.\u001a\u00020\b2\b\b\u0002\u0010&\u001a\u00020'J%\u0010/\u001a\u00020\u000e2\u0006\u0010.\u001a\u00020\b2\u0006\u0010&\u001a\u00020'2\u0006\u00100\u001a\u00020\u000eH\u0000¢\u0006\u0002\b1J\u0006\u0010\u001c\u001a\u00020\"J\b\u00102\u001a\u00020\u0005H\u0016R\u001c\u0010\u0007\u001a\u0004\u0018\u00010\bX\u0080\u000e¢\u0006\u000e\n\u0000\u001a\u0004\b\t\u0010\n\"\u0004\b\u000b\u0010\fR\u001a\u0010\r\u001a\u00020\u000eX\u0080\u000e¢\u0006\u000e\n\u0000\u001a\u0004\b\u000f\u0010\u0010\"\u0004\b\u0011\u0010\u0012R\u001a\u0010\u0013\u001a\b\u0012\u0004\u0012\u00020\b0\u0014X\u0080\u0004¢\u0006\b\n\u0000\u001a\u0004\b\u0015\u0010\u0016R\u0014\u0010\u0004\u001a\u00020\u0005X\u0080\u0004¢\u0006\b\n\u0000\u001a\u0004\b\u0017\u0010\u0018R\u0017\u0010\u0019\u001a\b\u0012\u0004\u0012\u00020\b0\u001a8F¢\u0006\u0006\u001a\u0004\b\u001b\u0010\u0016R\u001a\u0010\u001c\u001a\u00020\u000eX\u0080\u000e¢\u0006\u000e\n\u0000\u001a\u0004\b\u001d\u0010\u0010\"\u0004\b\u001e\u0010\u0012R\u0014\u0010\u0002\u001a\u00020\u0003X\u0080\u0004¢\u0006\b\n\u0000\u001a\u0004\b\u001f\u0010 \u0082\u0002\u0007\n\u0005\b\u009920\u0001¨\u00064"}, d2 = {"Lokhttp3/internal/concurrent/TaskQueue;", "", "taskRunner", "Lokhttp3/internal/concurrent/TaskRunner;", "name", "", "(Lokhttp3/internal/concurrent/TaskRunner;Ljava/lang/String;)V", "activeTask", "Lokhttp3/internal/concurrent/Task;", "getActiveTask$okhttp", "()Lokhttp3/internal/concurrent/Task;", "setActiveTask$okhttp", "(Lokhttp3/internal/concurrent/Task;)V", "cancelActiveTask", "", "getCancelActiveTask$okhttp", "()Z", "setCancelActiveTask$okhttp", "(Z)V", "futureTasks", "", "getFutureTasks$okhttp", "()Ljava/util/List;", "getName$okhttp", "()Ljava/lang/String;", "scheduledTasks", "", "getScheduledTasks", "shutdown", "getShutdown$okhttp", "setShutdown$okhttp", "getTaskRunner$okhttp", "()Lokhttp3/internal/concurrent/TaskRunner;", "cancelAll", "", "cancelAllAndDecide", "cancelAllAndDecide$okhttp", "execute", "delayNanos", "", "cancelable", "block", "Lkotlin/Function0;", "idleLatch", "Ljava/util/concurrent/CountDownLatch;", "schedule", "task", "scheduleAndDecide", "recurrence", "scheduleAndDecide$okhttp", "toString", "AwaitIdleTask", "okhttp"}, k = 1, mv = {1, 6, 0}, xi = 48)
public final class TaskQueue {
    public final TaskRunner a;

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

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

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

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final List<Task> f9728e;

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

    public TaskQueue(TaskRunner taskRunner, String str) {
        o.f(taskRunner, "taskRunner");
        o.f(str, "name");
        this.a = taskRunner;
        this.f9725b = str;
        this.f9728e = new ArrayList();
    }

    public final void a() {
        byte[] bArr = c.a;
        synchronized (this.a) {
            if (b()) {
                this.a.e(this);
            }
        }
    }

    public final boolean b() {
        Task task = this.f9727d;
        if (task != null) {
            o.c(task);
            if (task.f9721b) {
                this.f9729f = true;
            }
        }
        boolean z2 = false;
        int size = this.f9728e.size() - 1;
        if (size >= 0) {
            while (true) {
                int i2 = size - 1;
                if (this.f9728e.get(size).f9721b) {
                    Task task2 = this.f9728e.get(size);
                    Objects.requireNonNull(TaskRunner.a);
                    if (TaskRunner.f9731c.isLoggable(Level.FINE)) {
                        kotlin.reflect.w.internal.y0.n.c2.c.d(task2, this, "canceled");
                    }
                    this.f9728e.remove(size);
                    z2 = true;
                }
                if (i2 < 0) {
                    break;
                }
                size = i2;
            }
        }
        return z2;
    }

    public final void c(Task task, long j2) {
        o.f(task, "task");
        synchronized (this.a) {
            if (!this.f9726c) {
                if (d(task, j2, false)) {
                    this.a.e(this);
                }
            } else if (task.f9721b) {
                TaskRunner.b bVar = TaskRunner.a;
                if (TaskRunner.f9731c.isLoggable(Level.FINE)) {
                    kotlin.reflect.w.internal.y0.n.c2.c.d(task, this, "schedule canceled (queue is shutdown)");
                }
            } else {
                TaskRunner.b bVar2 = TaskRunner.a;
                if (TaskRunner.f9731c.isLoggable(Level.FINE)) {
                    kotlin.reflect.w.internal.y0.n.c2.c.d(task, this, "schedule failed (queue is shutdown)");
                }
                throw new RejectedExecutionException();
            }
        }
    }

    public final boolean d(Task task, long j2, boolean z2) {
        String strN;
        String str;
        o.f(task, "task");
        o.f(this, "queue");
        TaskQueue taskQueue = task.f9722c;
        if (taskQueue != this) {
            if (!(taskQueue == null)) {
                throw new IllegalStateException("task is in multiple queues".toString());
            }
            task.f9722c = this;
        }
        long jC = this.a.f9732d.c();
        long j3 = jC + j2;
        int iIndexOf = this.f9728e.indexOf(task);
        if (iIndexOf != -1) {
            if (task.f9723d <= j3) {
                TaskRunner.b bVar = TaskRunner.a;
                if (TaskRunner.f9731c.isLoggable(Level.FINE)) {
                    kotlin.reflect.w.internal.y0.n.c2.c.d(task, this, "already scheduled");
                }
                return false;
            }
            this.f9728e.remove(iIndexOf);
        }
        task.f9723d = j3;
        TaskRunner.b bVar2 = TaskRunner.a;
        if (TaskRunner.f9731c.isLoggable(Level.FINE)) {
            long j4 = j3 - jC;
            if (z2) {
                strN = kotlin.reflect.w.internal.y0.n.c2.c.N(j4);
                str = "run again after ";
            } else {
                strN = kotlin.reflect.w.internal.y0.n.c2.c.N(j4);
                str = "scheduled after ";
            }
            kotlin.reflect.w.internal.y0.n.c2.c.d(task, this, o.l(str, strN));
        }
        Iterator<Task> it = this.f9728e.iterator();
        int size = 0;
        while (true) {
            if (!it.hasNext()) {
                size = -1;
                break;
            }
            if (it.next().f9723d - jC > j2) {
                break;
            }
            size++;
        }
        if (size == -1) {
            size = this.f9728e.size();
        }
        this.f9728e.add(size, task);
        return size == 0;
    }

    public final void e() {
        byte[] bArr = c.a;
        synchronized (this.a) {
            this.f9726c = true;
            if (b()) {
                this.a.e(this);
            }
        }
    }

    /* JADX INFO: renamed from: toString, reason: from getter */
    public String getF9725b() {
        return this.f9725b;
    }
}
