package n7;

import P6.g;
import java.util.ArrayList;
import java.util.concurrent.RejectedExecutionException;
import java.util.logging.Level;
import l6.C1139a;

/* JADX INFO: loaded from: classes.dex */
public final class c {

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

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

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

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

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

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

    public c(d dVar, String str) {
        g.e(dVar, "taskRunner");
        g.e(str, "name");
        this.f15536a = dVar;
        this.f15537b = str;
        this.f15540e = new ArrayList();
    }

    public final void a() {
        byte[] bArr = l7.b.f14412a;
        synchronized (this.f15536a) {
            if (b()) {
                this.f15536a.d(this);
            }
        }
    }

    public final boolean b() {
        a aVar = this.f15539d;
        if (aVar != null && aVar.f15531b) {
            this.f15541f = true;
        }
        ArrayList arrayList = this.f15540e;
        boolean z8 = false;
        for (int size = arrayList.size() - 1; -1 < size; size--) {
            if (((a) arrayList.get(size)).f15531b) {
                a aVar2 = (a) arrayList.get(size);
                if (d.i.isLoggable(Level.FINE)) {
                    s7.d.a(aVar2, this, "canceled");
                }
                arrayList.remove(size);
                z8 = true;
            }
        }
        return z8;
    }

    public final void c(a aVar, long j8) {
        g.e(aVar, "task");
        synchronized (this.f15536a) {
            if (!this.f15538c) {
                if (d(aVar, j8, false)) {
                    this.f15536a.d(this);
                }
            } else if (aVar.f15531b) {
                if (d.i.isLoggable(Level.FINE)) {
                    s7.d.a(aVar, this, "schedule canceled (queue is shutdown)");
                }
            } else {
                if (d.i.isLoggable(Level.FINE)) {
                    s7.d.a(aVar, this, "schedule failed (queue is shutdown)");
                }
                throw new RejectedExecutionException();
            }
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:18:0x0047  */
    /* JADX WARN: Removed duplicated region for block: B:24:0x006b  */
    /* JADX WARN: Removed duplicated region for block: B:30:0x0081  */
    /* JADX WARN: Removed duplicated region for block: B:33:0x008a A[RETURN] */
    /* JADX WARN: Removed duplicated region for block: B:38:0x007e A[SYNTHETIC] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final boolean d(a aVar, long j8, boolean z8) {
        int size;
        int size2;
        int i;
        g.e(aVar, "task");
        c cVar = aVar.f15532c;
        if (cVar != this) {
            if (cVar != null) {
                throw new IllegalStateException("task is in multiple queues");
            }
            aVar.f15532c = this;
        }
        C1139a c1139a = this.f15536a.f15543a;
        long jNanoTime = System.nanoTime();
        long j9 = jNanoTime + j8;
        ArrayList arrayList = this.f15540e;
        int iIndexOf = arrayList.indexOf(aVar);
        if (iIndexOf == -1) {
            aVar.f15533d = j9;
            if (d.i.isLoggable(Level.FINE)) {
                s7.d.a(aVar, this, z8 ? "run again after ".concat(s7.d.o(j9 - jNanoTime)) : "scheduled after ".concat(s7.d.o(j9 - jNanoTime)));
            }
            size = arrayList.size();
            size2 = 0;
            i = 0;
            while (true) {
                if (i < size) {
                    size2 = -1;
                    break;
                }
                Object obj = arrayList.get(i);
                i++;
                if (((a) obj).f15533d - jNanoTime > j8) {
                    break;
                }
                size2++;
            }
            if (size2 == -1) {
                size2 = arrayList.size();
            }
            arrayList.add(size2, aVar);
            if (size2 != 0) {
                return true;
            }
        } else if (aVar.f15533d > j9) {
            arrayList.remove(iIndexOf);
            aVar.f15533d = j9;
            if (d.i.isLoggable(Level.FINE)) {
            }
            size = arrayList.size();
            size2 = 0;
            i = 0;
            while (true) {
                if (i < size) {
                }
                size2++;
            }
            if (size2 == -1) {
            }
            arrayList.add(size2, aVar);
            if (size2 != 0) {
            }
        } else if (d.i.isLoggable(Level.FINE)) {
            s7.d.a(aVar, this, "already scheduled");
            return false;
        }
        return false;
    }

    public final void e() {
        byte[] bArr = l7.b.f14412a;
        synchronized (this.f15536a) {
            this.f15538c = true;
            if (b()) {
                this.f15536a.d(this);
            }
        }
    }

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