package defpackage;

import com.google.android.gms.common.internal.Preconditions;
import java.util.ArrayDeque;
import java.util.concurrent.Executor;
import java.util.concurrent.RejectedExecutionException;
import java.util.logging.Logger;

/* JADX INFO: loaded from: classes2.dex */
final class ai8 implements Executor {
    public static final Logger f = Logger.getLogger(ai8.class.getName());
    public final Executor a;
    public final ArrayDeque b = new ArrayDeque();
    public b c = b.a;
    public long d = 0;
    public final a e = new a();

    public final class a implements Runnable {
        public Runnable a;

        public a() {
        }

        /* JADX WARN: Code restructure failed: missing block: B:18:0x003d, code lost:
        
            if (r1 == false) goto L50;
         */
        /* JADX WARN: Code restructure failed: missing block: B:23:0x0046, code lost:
        
            r1 = r1 | java.lang.Thread.interrupted();
            r2 = null;
         */
        /* JADX WARN: Code restructure failed: missing block: B:24:0x0048, code lost:
        
            r9.a.run();
         */
        /* JADX WARN: Code restructure failed: missing block: B:29:0x0052, code lost:
        
            r0 = move-exception;
         */
        /* JADX WARN: Code restructure failed: missing block: B:31:0x0054, code lost:
        
            r3 = move-exception;
         */
        /* JADX WARN: Code restructure failed: missing block: B:32:0x0055, code lost:
        
            defpackage.ai8.f.log(java.util.logging.Level.SEVERE, "Exception while executing runnable " + r9.a, (java.lang.Throwable) r3);
         */
        /* JADX WARN: Code restructure failed: missing block: B:34:0x0070, code lost:
        
            r9.a = null;
         */
        /* JADX WARN: Code restructure failed: missing block: B:35:0x0072, code lost:
        
            throw r0;
         */
        /* JADX WARN: Code restructure failed: missing block: B:50:?, code lost:
        
            return;
         */
        /* JADX WARN: Removed duplicated region for block: B:46:0x0036 A[SYNTHETIC] */
        /*
            Code decompiled incorrectly, please refer to instructions dump.
        */
        public final void a() {
            Runnable runnable;
            boolean z = false;
            boolean zInterrupted = false;
            while (true) {
                try {
                    synchronized (ai8.this.b) {
                        if (z) {
                            runnable = (Runnable) ai8.this.b.poll();
                            this.a = runnable;
                            if (runnable != null) {
                            }
                        } else {
                            ai8 ai8Var = ai8.this;
                            b bVar = ai8Var.c;
                            b bVar2 = b.d;
                            if (bVar != bVar2) {
                                ai8Var.d++;
                                ai8Var.c = bVar2;
                                z = true;
                                runnable = (Runnable) ai8.this.b.poll();
                                this.a = runnable;
                                if (runnable != null) {
                                    ai8.this.c = b.a;
                                }
                            }
                        }
                    }
                    if (!zInterrupted) {
                        return;
                    }
                } finally {
                    if (zInterrupted) {
                        Thread.currentThread().interrupt();
                    }
                }
            }
        }

        @Override // java.lang.Runnable
        public final void run() {
            try {
                a();
            } catch (Error e) {
                synchronized (ai8.this.b) {
                    ai8.this.c = b.a;
                    throw e;
                }
            }
        }

        public final String toString() {
            Runnable runnable = this.a;
            if (runnable != null) {
                return "SequentialExecutorWorker{running=" + runnable + "}";
            }
            return "SequentialExecutorWorker{state=" + ai8.this.c + "}";
        }
    }

    /* JADX WARN: Failed to restore enum class, 'enum' modifier and super class removed */
    /* JADX WARN: Unknown enum class pattern. Please report as an issue! */
    public static final class b {
        public static final b a;
        public static final b b;
        public static final b c;
        public static final b d;
        public static final /* synthetic */ b[] e;

        static {
            b bVar = new b("IDLE", 0);
            a = bVar;
            b bVar2 = new b("QUEUING", 1);
            b = bVar2;
            b bVar3 = new b("QUEUED", 2);
            c = bVar3;
            b bVar4 = new b("RUNNING", 3);
            d = bVar4;
            e = new b[]{bVar, bVar2, bVar3, bVar4};
        }

        public static b valueOf(String str) {
            return (b) Enum.valueOf(b.class, str);
        }

        public static b[] values() {
            return (b[]) e.clone();
        }
    }

    public ai8(Executor executor) {
        this.a = (Executor) Preconditions.checkNotNull(executor);
    }

    /* JADX WARN: Removed duplicated region for block: B:43:0x0061  */
    @Override // java.util.concurrent.Executor
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void execute(Runnable runnable) {
        b bVar;
        boolean z;
        Preconditions.checkNotNull(runnable);
        synchronized (this.b) {
            b bVar2 = this.c;
            if (bVar2 != b.d && bVar2 != (bVar = b.c)) {
                long j = this.d;
                xh8 xh8Var = new xh8(runnable);
                this.b.add(xh8Var);
                b bVar3 = b.b;
                this.c = bVar3;
                try {
                    this.a.execute(this.e);
                    if (this.c != bVar3) {
                        return;
                    }
                    synchronized (this.b) {
                        try {
                            if (this.d == j && this.c == bVar3) {
                                this.c = bVar;
                            }
                        } finally {
                        }
                    }
                    return;
                } catch (Error | RuntimeException e) {
                    synchronized (this.b) {
                        try {
                            b bVar4 = this.c;
                            if (bVar4 == b.a || bVar4 == b.b) {
                                z = this.b.removeLastOccurrence(xh8Var);
                            }
                            if (!(e instanceof RejectedExecutionException) || z) {
                                throw e;
                            }
                        } finally {
                        }
                    }
                    return;
                }
            }
            this.b.add(runnable);
        }
    }

    public final String toString() {
        return "SequentialExecutor@" + System.identityHashCode(this) + "{" + this.a + "}";
    }
}
